Error: "[SC] StartService FAILED 1053" while starting a windows service

Error: "[SC] StartService FAILED 1053" while starting a windows service

我在 vs2019 中创建了一个 worker 服务,但遇到错误“[SC] StartService FAILED 1053”。

先说说我是怎么做到的

1、创建工人服务项目

2、发布项目

3,运行 cmd.exe 作为管理员

4、创建windows服务“testworker”并启动

sc Create testworker BinPath="c:\Users\zxi.BS\source\repos\WorkerService1\WorkerService1\bin\Release\net5.0\publish\WorkerService1.exe"
sc start testworker

然后我遇到了这个错误。 exe 运行 如果我直接 运行 它就很好。

为什么我会收到此错误:

[SC] StartService FAILED 1053:

The service did not respond to the start or control request in a timely fashion.

出现错误 1053 是因为您创建的应用程序不是真正的 Windows 服务。仅扩展 BackgroundService class 来创建服务是不够的 — 改为使用 ServiceBase

This tutorial 概述了要采取的方法。

我目前 运行 正在处理这个问题。但我敢肯定,在您的情况下,您缺少的是 Nuget 包 Microsoft.Extensions.Hosting.WindowsServices。然后您必须将 UseWindowsService() 添加到您的 HostBuilder:

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

来源:https://github.com/dotnet/extensions/issues/2496