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

ASP.NET Core 6 Windows Service无法启动及Wix安装故障求助

ASP.NET Core 6 WebAPI Windows服务Wix安装启动失败问题

项目配置

创建了简化的ASP.NET Core 6 WebAPI项目,以Windows Service形式托管,仅通过示例WeatherForecast控制器验证运行状态。已添加Microsoft.Extensions.Hosting(6.0.1)和Microsoft.Extensions.Hosting.WindowsServices(6.0.0) NuGet包。

Program.cs代码

using IRDSPrototype.Configuration;
using IRDSPrototype.Services;
using Microsoft.Extensions.Hosting.WindowsServices;

var webApplicationOptions = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(webApplicationOptions);

builder.Services.AddHostedService<IRDSHostingService>();
builder.Host.UseWindowsService();

builder.WebHost.UseUrls(UriServices.GetBaseUri().AbsoluteUri);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

await app.RunAsync();

后台服务IRDSHostingService代码

public class IRDSHostingService : BackgroundService
{
    public ILogger Logger { get; }

    public IRDSHostingService(ILoggerFactory loggerFactory)
    {
        Logger = loggerFactory.CreateLogger<IRDSHostingService>();
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        Logger.LogInformation("IRDS Hosting Service is starting.");

        stoppingToken.Register(() => Logger.LogInformation("IRDS Hosting Service is stopping."));

        while (!stoppingToken.IsCancellationRequested)
        {
            await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
        }

        Logger.LogInformation("IRDS Hosting Service has stopped.");
    }
}

问题排查历程

  • Wix安装程序启动服务失败:执行到“启动服务”步骤时提示请确认您拥有足够权限启动系统服务,Windows应用日志显示文件未找到异常,无法加载System.ServiceProcess.ServiceController程序集(发布目录和Wix清单已包含该DLL)。
  • sc.exe安装服务:安装成功但启动时出现Error 5: 拒绝访问,且未生成Windows日志信息。已为程序所在文件夹授予NETWORK SERVICE和LOCAL SERVICE完全权限,问题未解决。
  • 修正程序集问题:确认需使用.NET 6.0.0运行时runtimes文件夹中的System.ServiceProcess.ServiceController.dll版本,文件未找到错误消失,但仍出现以下错误:

Error 1920. Service 'IRDS Prototype' (IRDSPrototype) failed to start. Verify that you have sufficient privileges to start system services
设置服务以Local Service身份运行,并为对应文件夹授予完全权限,问题仍存在。

  • sc.exe安装修正:发现sc.exe安装时的binPath未包含exe文件,修正后sc.exe安装的服务可正常启动运行,但Wix安装的服务依旧无法启动,寻求Wix安装Windows服务的解决办法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 18:06:27