我正在使用 .NET 5,我想使用IHostedServiceclassess 運行后臺任務,如您所見:
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
}
所以我必須將它注入到服務集合中,如您所見:
services.AddHostedService<PasargadJobs>();
但是在添加這個之后我得到了這個錯誤:
System.AggregateException:'某些服務無法構造(驗證服務描述符時出錯'ServiceType:Microsoft.Extensions.Hosting.IHostedService Lifetime:Singleton ImplementationType:MIMS.Portal.ScheduleJobs.PasargadJobs':無法使用范圍服務'Microsoft .EntityFrameworkCore.DbContextOptions`1[MIMS.Portal.Infrustructure.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>();
uj5u.com熱心網友回復:
您的托管服務是單例的,并且您間接依賴于范圍服務:
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>();
不要將CreateScopeandGetRequiredService放在托管服務的建構式中:由于托管服務是單例的,建構式只會被呼叫一次,因此創建的實體scope.ServiceProvider.GetRequiredService<T>()也將被創建一次。因此,在這種情況下,您將使用被設計為短暫存在的服務作為永遠活著的單身人士。使用后也不要忘記丟棄scope。
由于看起來您正在使用 Entity Framework Core 和上述方法的替代方案,您還可以考慮重構要使用的服務,IDbContextFactory<TContext> 以便將它們注冊為單例服務。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429506.html
標籤:asp.net 核心 依赖注入 后台任务 asp.net-core-hosted-services 服务集合
上一篇:物體框架:“dotnetef資料庫更新”后,資料庫未顯示在SQLServerManagementStudio中
下一篇:如何對字典串列中的專案進行排序
