Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton code example
Example: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType:
public class MySingletonService
{
private IServiceScopeFactory Services { get; }
public MySingletonService(IServiceScopeFactory services)
{
Services = services;
}
public void DoSomethingWithScopedService()
{
using (var scope = Services.CreateScope())
{
var myScopedService = scope.GetRequiredService<IScopedService>();
}
}
}