我在運行我的應用程式時偶然發現了這個錯誤。
System.AggregateException: '無法構建某些服務(驗證服務描述符時出錯:'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: testing.CacheUpdater': 無法決議型別為 'testing.CacheMonitorOptions 的服務' 同時嘗試激活 'testing.CacheUpdater'。
應用程式說明 我正在制作一個應用程式,我定期(每 10 秒)使用我從資料庫中獲取的值更新 MemoryCache。
為此,我使用了 3 個類,CacheMonitor(負責更新/覆寫快取)、StudentsContext(負責從資料庫中獲取資料)和 CacheUpdater,它是一個后臺服務,在 CacheMonitor 中呼叫 Update 方法班級。
我已經像這樣將它們注入到我的 DI 容器中:
啟動檔案
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddHostedService<CacheUpdater>();
services.AddDbContext<StudentsContext>(options =>
{
options.UseSqlServer(Configuration["Database:ConnectionString"]);
});
services.Configure<CacheMonitorOptions>(Configuration.GetSection("CacheUpdater"));
services.AddTransient<ICacheMonitor, CacheMonitor>();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "testing", Version = "v1" });
});
}
快取監視器.cs
public class CacheMonitor : ICacheMonitor
{
private readonly IMemoryCache _cache;
private readonly ILogger<CacheMonitor> _logger;
private readonly StudentContext _databaseContext;
public CacheMonitor(
IMemoryCache cache,
IOptions<CacheMonitor> options,
StudentContext context,
ILogger<CacheMonitor> logger)
{
this._cache = cache;
this._databaseContext = context;
this._logger = logger;
}
public void UpdateCache()
{
//updates cache
}
}
快取更新程式
public class CacheUpdater{
private readonly ICacheMonitor _cacheMonitor;
private readonly CacheMonitorOptions _cacheMonitorOptions;
private readonly ILogger<CacheUpdater> _logger;
public CacheUpdater(
ICacheMonitor cacheMonitor,
CacheMonitorOptions cacheMonitorOptions,
ILogger<CacheUpdater> logger)
{
_cacheMonitor = cacheMonitor;
_cacheMonitorOptions = cacheMonitorOptions;
_logger = logger;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation($"trying to update cache");
_cacheMonitor.UpdateCache();
Thread.Sleep(_cacheMonitorOptions.Interval);
return Task.CompletedTask;
}
}
我知道它必須與服務的生命周期有關,但我不確定如何修復它。
uj5u.com熱心網友回復:
將CacheUpdaterctor更改為接受IOptions<CacheMonitorOptions>而不是選項(對其他代碼進行相應更改):
public CacheUpdater(
ICacheMonitor cacheMonitor,
IOptions<CacheMonitorOptions> cacheMonitorOptions,
ILogger<CacheUpdater> logger
)
{
...
}
另請查看檔案。
UPD
解決評論中的問題 - 如果您不想在檔案中使用定時后臺任務中的模式,您可以沿著這條線做一些事情(未測驗):
public class CacheUpdater
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly CacheMonitorOptions _cacheMonitorOptions;
private readonly ILogger<CacheUpdater> _logger;
public CacheUpdater(
IServiceScopeFactory scopeFactory,
CacheMonitorOptions cacheMonitorOptions,
ILogger<CacheUpdater> logger
)
{
_scopeFactory = scopeFactory;
_cacheMonitorOptions = cacheMonitorOptions;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while(!stoppingToken.IsCancellationRequested) // !
{
_logger.LogInformation($"trying to update cache");
using (var scope = _serviceScopeFactory.CreateScope())
{
var cacheMonitor = scope.ServiceProvider.GetService<ICacheMonitor>();
cacheMonitor.UpdateCache();
await Task.Delay(_cacheMonitorOptions.Interval, stoppingToken); // DO NOT USE THREAD SLEEP HERE!
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338683.html
上一篇:如何使用R合并冗余資訊?
下一篇:C#日期時間和字串值條件檢查
