我正在使用 .NET Core 2.2,并且我想运行长时间运行的后台任务(如旧的 Windows 服务)。我想使用Microsoft.Extensions.Hosting.BackgroundService(在Microsoft.Extensions.Hosting.Abstractions装配中)。
我的问题是我的服务立即启动和停止。我写了这个小样本来展示我所做的:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MyHostedService
{
internal static class Program
{
private static void Main()
{
new HostBuilder()
.ConfigureServices((hostContext, services) => { services.AddHostedService<MyService>(); })
.Build()
.RunAsync();
}
}
public class MyService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
int count = 0;
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine($"Hit count: {++count}, IsCancellationRequested: {stoppingToken.IsCancellationRequested}");
await Task.Delay(TimeSpan.FromSeconds(1));
}
Console.WriteLine($"IsCancellationRequested: {stoppingToken.IsCancellationRequested}");
Console.WriteLine("The end.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
> Hit count: 1, IsCancellationRequested: False
Application started. Press Ctrl+C to shut down.
Hosting environment: Production
Content root path: C:\Users\(personnal path hidden)\MyHostedService\MyHostedService\bin\Debug\netcoreapp2.2\
> IsCancellationRequested: True
> The end.
C:\Program Files\dotnet\dotnet.exe (process 31088) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Run Code Online (Sandbox Code Playgroud)
我保证,我没有按 Ctrl+C,也没有按退出键。
我错过了什么吗?您能否告诉我要更改哪些内容以确保该服务能够运行,直到我按 Ctrl+C 停止它为止?
我期望得到如下结果:
Application started. Press Ctrl+C to shut down.
Hosting environment: Production
Content root path: C:\Users\(personnal path hidden)\MyHostedService\MyHostedService\bin\Debug\netcoreapp2.2\
> Hit count: 1, IsCancellationRequested: False
> Hit count: 2, IsCancellationRequested: False
> Hit count: 3, IsCancellationRequested: False
> Hit count: 4, IsCancellationRequested: False
> Hit count: 5, IsCancellationRequested: False
> Hit count: 6, IsCancellationRequested: False
> Hit count: 7, IsCancellationRequested: False
> Hit count: 8, IsCancellationRequested: False
> Hit count: 9, IsCancellationRequested: False
> Hit count: 10, IsCancellationRequested: False
> Hit count: 11, IsCancellationRequested: False
Run Code Online (Sandbox Code Playgroud)
...(现在我按 Ctrl+C )...
> IsCancellationRequested: True
> The end.
C:\Program Files\dotnet\dotnet.exe (process 31088) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Run Code Online (Sandbox Code Playgroud)
您异步运行它,因此一旦主函数到达末尾,应用程序就会退出。这就是请求取消的地方。尝试等待服务:
private async Task Main()
{
await new HostBuilder()
.ConfigureServices((hostContext, services) => { services.AddHostedService<MyService>(); })
.Build()
.RunAsync();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3819 次 |
| 最近记录: |