asp.net core host service

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>();

注意: 如果Host service中抛出异常,那么整个应用就会终止

上一篇:【VS2022+ASP.Net Core+.net6】Log4net 配置和使用


下一篇:wget爬取网站