Is there a way in ASP.NET to get all singletons?
I want to implement a health check which would instantiate all the registered singletons and only after that return a healthy status.
I am trying to get all the singletons from the IServiceProvider
, but it seems to have only one method, i.e. the object? GetService(Type serviceType);
.
So, maybe someone knows a way in C# to get all singletons?
I am trying to do like so:
_servicesProvider
.GetServices<object>()
.Where(service => _servicesProvider.GetService(service.GetType()) == service)
But it seems to also give me back scoped services.
Seems like this:
_servicesProvider
.GetServices<object>()
.ToList();
return new HealthCheckResult(HealthStatus.Healthy);
should work. But I am still hoping for a more optimal solution.