I\'m registering a service as a singleton in .NET Core. Yet I\'m seeing the constructor for the singleton called multiple times.
services.AddSingleton
You can declare a dependency on IServiceProvider
-- don't build it, inject it.
public class SomeController
{
DbAuthorizationOptions authOptions;
public SomeController(IServiceProvider provider)
{
authOptions = provider.GetSerivce();
}
}
But this is the service locator anti-pattern. As I commented on NightOwl888's post after you gave more details, a factory is probably a better approach.