You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Worker Service部署为Windows服务启动失败求助

解决.NET Worker Service注册为Windows服务后启动失败的问题

事件查看器错误摘要

  1. TestWorkerService服务启动失败,错误信息:服务未及时响应启动或控制请求。
  2. 故障应用程序名称:TestWorkerService.exe,版本:1.0.0.0,时间戳:0x6243f19c
    故障模块名称:KERNELBASE.dll,版本:6.1.7601.24384,时间戳:0x5c6e248c
    异常代码:0xe0434352
    故障偏移量:0x000000000000be0d
    故障进程ID:0x8778
    故障应用程序启动时间:0x01d9409075d9a7b9
    故障应用程序路径:C:\inetpub\TestWorkerService\TestWorkerService.exe
    故障模块路径:C:\Windows\system32\KERNELBASE.dll
    报告ID:b3e4df75-ac83-11ed-a1fc-000c290f795a
  3. 应用程序:TestWorkerService.exe
    CoreCLR版本:4.700.22.30802
    .NET Core版本:3.1.27
    描述:进程因未处理的异常而终止。
    异常信息:System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems.
    at System.ServiceProcess.ServiceBase..ctor()
    at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceLifetime..ctor(IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions1 optionsAccessor, IOptions1 windowsServiceOptionsAccessor)
    ...(省略后续调用栈)

错误核心

尽管运行在Windows系统上,但.NET运行时错误识别了操作系统环境,或服务的发布/配置不符合Windows服务要求,导致触发System.PlatformNotSupportedException异常。

解决方案

1. 强制指定Windows作为发布目标平台

发布Worker Service时必须明确指定Windows平台,不能使用跨平台的Any CPU配置:

  • 命令行发布示例:
    dotnet publish -c Release -r win-x64 --self-contained false
    
    (--self-contained false依赖系统已安装的.NET Runtime,若要生成独立可执行文件改为true)
  • 或在项目文件(.csproj)中添加固定运行时标识:
    <PropertyGroup>
      <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    </PropertyGroup>
    

2. 验证Windows服务依赖与配置

  • 确保项目已安装Microsoft.Extensions.Hosting.WindowsServices NuGet包,版本需与.NET Core版本匹配(如.NET 3.1对应3.1.x版本)
  • 在Program.cs中必须调用UseWindowsService(),启用Windows服务生命周期管理:
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseWindowsService() // 关键配置,不可省略
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
    

3. 安装.NET Desktop Runtime

Windows服务依赖.NET Desktop Runtime组件,而非仅Core Runtime:

  • 前往微软官网下载对应版本的.NET Desktop Runtime(如.NET 3.1版本)
  • 运行dotnet --list-runtimes验证,确保输出包含Microsoft.WindowsDesktop.App 3.1.x

4. 调整服务账户权限

  • 将服务的登录账户切换为本地管理员或域管理员账户,避免Local System账户权限不足引发的异常
  • 在services.msc中找到目标服务,右键→属性→登录,选择合适账户并输入密码

5. 控制台模式预调试

  • 直接双击发布后的TestWorkerService.exe,以控制台模式运行,查看实时输出的错误信息,快速定位问题
  • 在Worker.cs中添加文件日志,追踪服务启动流程:
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            File.AppendAllText("C:\\temp\\worker-service-log.txt", $"Worker active at: {DateTimeOffset.Now}{Environment.NewLine}");
            await Task.Delay(1000, stoppingToken);
        }
    }
    

内容的提问来源于stack exchange,提问作者Adomel Arabuli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 20:46:35