namespace wms.workservice { public class Worker : BackgroundService { private readonly ILogger _logger; private readonly ConService _consevice; public Worker(ILogger logger, ConService consevice) { _logger = logger; _consevice= consevice; } public override async Task StartAsync(CancellationToken cancellationToken) { _consevice.WriteLog("启动时间为: " + DateTimeOffset.Now); //_consevice.FJConsumer(); await base.StartAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _consevice.WriteLog("执行时间为: " + DateTimeOffset.Now); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(10000, stoppingToken); } } //停止时执行 public override async Task StopAsync(CancellationToken cancellationToken) { _consevice.WriteLog("停止时间为: " + DateTimeOffset.Now); await base.StopAsync(cancellationToken); } } }