无法停止工作者服务,因为它不存在

时间:2019-11-22 13:58:51

标签: c# asp.net-core .net-core windows-services

我已经创建了一个.net core 3.0辅助服务,它实际上只是您开始使用的模板

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
}

我也有Worker.cs类,它也是模板

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger)
    {
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            await Task.Delay(10000, stoppingToken);
        }
    }
}

然后我运行脚本来创建服务

dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true

我已经运行了exe并完成了CTRL + C来关闭它。但是,当我回到解决方案时,它不会建立。我得到

Could not copy "*" to "*"Beginning retry 1 in 1000ms. The Processcannoy access the file "*.exe"  
because it is being used by another process. the file is locked by "*"

我已经检查了我的服务,但没有与在其中运行的项目相同的服务名称。如何停止服务,以便我可以继续从事该项目?

0 个答案:

没有答案