host service
public class HostService01 : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine("HostService01 started.");
await Task.Delay(10000); // 不要使用Thread.Sleep()
Console.WriteLine("hello world!!!");
await Task.Delay(10000);
Console.WriteLine("HostService01 stopped.");
}
}
注册host service
builder.Services.AddHostedService<HostService01>();