验证服务描述符服务型时类型时,工人服务错误
我有一个在.NET Core 3.1中写的工人服务。在这里,我想在我的program.cs中在另一层(icarverticallogic和iemailpdflogic)中注册服务,但是当我运行项目时,我会收到以下错误。
我的program.cs
public class Program
{
public static void Main(string[] args)
{
var startupPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName);
var logFilePath = Path.Combine(startupPath, "logs", "logs.txt");
Log.Logger = new LoggerConfiguration()
.WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day)
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureLogging(logging =>
{
logging.AddSerilog();
})
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton(AutoMapperConfig.CreateMapper());
services.AddScoped<ICarVerticalLogic, CarVerticalLogic>();
services.AddScoped<IEmailPdfLogic, EmailPdfLogic>();
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
services.AddEntityFrameworkSqlServer()
.AddDbContext<AsamDbContext>(options =>
{
options.UseMySql(hostContext.Configuration.GetSection("ConnectionString").Value);
});
services.AddHostedService<Worker>();
});
}
错误:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: CarVerticalWorkerService.Worker': Cannot consume scoped service 'Asam.Logic.Infrastructure.ICarVerticalLogic' from singleton 'Microsoft.Extensions.Hosting.IHostedService'.
I have a worker service that I wrote in .Net Core 3.1. Here I want to register services in another layer (ICarVerticalLogic and IEmailPdfLogic) in my program.cs, but when I run the project I get the following error.
My Program.cs
public class Program
{
public static void Main(string[] args)
{
var startupPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName);
var logFilePath = Path.Combine(startupPath, "logs", "logs.txt");
Log.Logger = new LoggerConfiguration()
.WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day)
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureLogging(logging =>
{
logging.AddSerilog();
})
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton(AutoMapperConfig.CreateMapper());
services.AddScoped<ICarVerticalLogic, CarVerticalLogic>();
services.AddScoped<IEmailPdfLogic, EmailPdfLogic>();
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
services.AddEntityFrameworkSqlServer()
.AddDbContext<AsamDbContext>(options =>
{
options.UseMySql(hostContext.Configuration.GetSection("ConnectionString").Value);
});
services.AddHostedService<Worker>();
});
}
Error :
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: CarVerticalWorkerService.Worker': Cannot consume scoped service 'Asam.Logic.Infrastructure.ICarVerticalLogic' from singleton 'Microsoft.Extensions.Hosting.IHostedService'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,工人服务是单身服务。并且范围的服务实例注定要请求。您无法将范围的服务注入Singleton服务。
如果您不介意改变icarverticallogic和iemailpdflogic服务的寿命
对于Singleton,请尝试修改您的代码:
对
我的测试结果:

The worker service is a singleton service by default. And a scoped service instances are bound to request. You can not inject a scoped service into a singleton service.
If you don't mind change the lifetime of ICarVerticalLogic and IEmailPdfLogic service
to Singleton, try to modify your codes:
to
And here's my test result:

或者,对于Ruikai答案,您可以将服务提供商注入构造函数:
然后创建一个范围。在Singleton中注入范围的服务:
Alternatively to Ruikai answer you can inject the service provider in the constructor:
Then create a scope. To inject the scoped service in the singleton: