【问题标题】:ASP.NET Core .NET 6 Preview 7 Windows ServiceASP.NET Core .NET 6 预览版 7 Windows 服务
【发布时间】:2021-11-06 11:33:09
【问题描述】:

我使用 Visual Studio 2022 Preview 创建了一个新的 ASP.NET Core 项目,并尝试将其作为 Windows 服务运行。我下载了最新的 Microsoft.Extensions.Hosting.WindowsServices 包(6.0.0-preview.7.21377.19)。

在线研究时,函数 .UseWindowsService() 进入 CreateHostBuilder 方法。但在新模板中,它看起来有所不同。我不明白应该在新模板中调用 .UseWindowsService 的位置。这是我当前的代码,看起来服务正在启动,但是当我浏览到 localhost:5000 时,它给了我 404 错误

using Microsoft.OpenApi.Models;
using Microsoft.Extensions.Hosting.WindowsServices;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseWindowsService(); // <--- Added this line

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "MyWindowsService", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
        app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyWindowsService v1"));
}

app.UseAuthorization();

app.MapControllers();


app.Run();

我像这样发布了我的服务 exe

dotnet publish -c Release -r win-x64 --self-contained

【问题讨论】:

  • 你能访问http://localhost:5000/swagger/v1/swagger.json吗?
  • 不,服务似乎也没有启动。 “MyService 服务因以下错误无法启动:服务未及时响应启动或控制请求。”
  • 错误似乎是这样的:CoreCLR Version: 6.0.21.45113 .NET Version: 6.0.0-rc.1.21451.13 描述:由于未处理的异常,进程被终止。异常信息:System.NotSupportedException:内容根已更改。 Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.UseContentRoot(IHostBuilder hostBuilder, String contentRoot) 的 Microsoft.AspNetCore.Builder.ConfigureHostBuilder.ConfigureHostConfiguration(Action`1 configureDelegate) 不支持更改主机配置
  • 你是如何创建这个项目的?如果你想在 Windows 服务中托管 ASP.NET Core,可以参考this document
  • @cvalueapp 你找到解决方案了吗?

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


【解决方案1】:

以下编码将生命周期设置为 WindowsServiceLifetime 并启用记录到事件日志。在大多数情况下,这就是您将应用程序作为 Windows 服务运行所需的全部内容。

if (WindowsServiceHelpers.IsWindowsService())
{
    appBuilder.Services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
    appBuilder.Logging.AddEventLog(settings =>
    {
        if (string.IsNullOrEmpty(settings.SourceName))
        {
            settings.SourceName = appBuilder.Environment.ApplicationName;
        }
    });
}

【讨论】:

    【解决方案2】:

    因为只是使用

    builder.Host.UseWindowsService();
    

    不适用于 WebApplication.CreateBuilder() (see),而是会抛出异常

    Exception Info: System.NotSupportedException: The content root changed from "C:\Windows\system32\" to "...". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
    

    或者更确切地说会导致这个错误

    Start-Service : Service 'Service1 (Service1)' cannot be started due to the following error: Cannot start service Service1 on computer '.'.
    

    当我尝试在 PowerShell 中使用 Start-Service 启动服务时,我发现了一个适合我的解决方法

    using Microsoft.Extensions.Hosting.WindowsServices;
    
    var options = new WebApplicationOptions
    {
        Args = args,
        ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
    };
    
    var builder = WebApplication.CreateBuilder(options);
    
    builder.Host.UseWindowsService();
    

    这里: An asp.net core web api application, using "UseWindowsService", reports an error “System.NotSupportedException: The content root changed. Changing the host configuration is not supported ” when starting the service

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 2018-03-15
      • 2021-11-17
      • 2019-12-20
      相关资源
      最近更新 更多