尝试使用:
启动.cs
public void ConfigureServices(IServiceCollection services) {
services.AddHostedService<LifetimeEvents>();
.
.
.
}
LifeTimeEvents 类从 IHostedService 继承。我收到此错误:
'IServiceCollection' does not contain a definition for 'AddHostedService' and no extension method 'AddHostedService' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
我似乎找不到要使用的正确命名空间或要包含的 nuget 包以使其正常工作,但它在 .NET Core 2.1 中开箱即用,这在 .NET Core 2.0 中是否不可用?有什么办法让它工作吗?
更新:
作为一种解决方法,我将代码更改为使用:
启动.cs
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<LifetimeEvents>();
.
.
.
}
public void Configure(IApplicationBuilder appBuilder, IHostingEnvironment envHost, LifetimeEvents appEvents) {
appEvents.StartAsync(new CancellationToken(false));
.
.
.
}
这似乎已经完成了这项工作。没有回答我最初的问题,我不确定它是多么“最佳实践”,但它确实让我开始重构这个 .NET Core 2.0 应用程序。
慕妹3242003
收到一只叮咚
相关分类