Ehs*_*bar 13 dependency-injection background-task asp.net-core asp.net-core-hosted-services servicecollection
我正在使用 .NET 5,我想使用IHostedService
类运行后台任务,如您所见:
public class PasargadJobs : IHostedService, IDisposable
{
private Timer _timer = null!;
readonly ITerminalService _terminalService;
readonly IMessageService _messageService;
readonly IMerchantService _merchantService;
public PasargadJobs(
IMerchantService merchantService,
ITerminalService terminalService,
IMessageService messageService)
{
_messageService = messageService;
_terminalService = terminalService;
_merchantService = merchantService;
}
//other parts of code are removed for short code
}
Run Code Online (Sandbox Code Playgroud)
所以我必须将它注入到服务集合中,如您所见:
services.AddHostedService<PasargadJobs>();
Run Code Online (Sandbox Code Playgroud)
但添加此后我收到此错误:
System.AggregateException:“无法构造某些服务(验证服务描述符时出错”ServiceType:Microsoft.Extensions.Hosting.IHostedService Lifetime:Singleton ImplementType:MIMS.Portal.ScheduleJobs.PasargadJobs”:无法使用作用域服务“Microsoft” .EntityFrameworkCore.DbContextOptions`1[MIMS.Portal.Infrustruct.Repositories.AppDbContext]'来自单例'Microsoft.Extensions.Hosting.IHostedService'。)'
这是我的startup.cs
services.AddExpressiveAnnotations();
//--- DB Context ---//
services.AddTransient<AppDbContext, AppDbContext>();
//--- Repositories ---//
services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddTransient<IMerchantRepository, MerchantRepository>();
services.AddTransient<IBankAccountRepository, BankAccountRepository>();
services.AddTransient<IBankRepository, BankRepository>();
services.AddTransient<IGeoRepository, GeoRepository>();
services.AddTransient<IDepartmentRepository, DepartmentRepository>();
services.AddTransient<IDeviceRepository, DeviceRepository>();
services.AddTransient<IInvoiceRepository, InvoiceRepository>();
services.AddTransient<IStoreRepository, StoreRepository>();
services.AddTransient<ITerminalRepository, TerminalRepository>();
services.AddTransient<ITicketRepository, TicketRepository>();
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IFileRepository, FileRepository>();
services.AddTransient<IPartnerRepository, PartnerRepository>();
services.AddTransient<IStoreScopeRepository, StoreScopeRepository>();
services.AddTransient<ICommentRepository, CommentRepository>();
services.AddTransient<INewsRepository, NewsRepository>();
services.AddTransient<IPosBrandRepository, PosBrandRepository>();
//-- Services --//
services.AddTransient<IMerchantService, MerchantService>();
services.AddTransient<IBankAccountService, BankAccountService>();
services.AddTransient<IBankService, BankService>();
services.AddTransient<IGeoService, GeoService>();
services.AddTransient<IDepartmentService, DepartmentService>();
services.AddTransient<IDeviceService, DeviceService>();
services.AddTransient<IInvoiceService, InvoiceService>();
services.AddTransient<IStoreService, StoreService>();
services.AddTransient<ITerminalService, TerminalService>();
services.AddTransient<ITicketService, TicketService>();
services.AddTransient<IUsersService, UsersService>();
services.AddTransient<IFilesManagerService, FilesManagerService>();
services.AddTransient<IMessageService, MessageService>();
services.AddTransient<IPartnerService, PartnerService>();
services.AddTransient<IStoreScopeService, StoreScopeService>();
services.AddTransient<INewsService, NewsService>();
services.AddTransient<ICommentService, CommentService>();
services.AddTransient<IPosBrandService, PosBrandService>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(option =>
{
option.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "localhost",
ValidAudience = "localhost",
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes("wevhgfrtyhasdfghjklzxcvbnm"))
};
});
services.AddHostedService<PasargadJobs>();
Run Code Online (Sandbox Code Playgroud)
Chr*_*eld 23
您的托管服务是单例的,并且您间接依赖于范围内的服务:
Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions`1[MIMS.Portal.Infrustructure.Repositories.AppDbContext]' from singleton 'Microsoft.Extensions.Hosting.IHostedService'
您可以通过以下方式解决该问题
注入IServiceScopeFactory
您的托管服务并手动创建服务范围,如下所示
using var scope = _serviceScopeFactory.CreateScope();
var service = scope.ServiceProvider.GetRequiredService<IScopedService>();
Run Code Online (Sandbox Code Playgroud)
不要将CreateScope
andGetRequiredService
放在托管服务的构造函数中:由于托管服务是单例,因此构造函数将仅被调用一次,因此创建的实例scope.ServiceProvider.GetRequiredService<T>()
也将被创建一次。因此,在这种情况下,您将使用被设计为短暂的服务作为永久的单例。scope
使用后也不要忘记丢弃。
由于看起来您正在使用 Entity Framework Core 和上述方法的替代方法,因此您还可以考虑重构要使用的服务,IDbContextFactory<TContext>
以便将它们注册为单例服务。
归档时间: |
|
查看次数: |
51145 次 |
最近记录: |