简体   繁体   English

ASP.NET Core .NET 6 Preview 7 Windows 服务

[英]ASP.NET Core .NET 6 Preview 7 Windows Service

I created a new ASP.NET Core project with Visual Studio 2022 Preview and I am trying to run it as a Windows Service.我使用 Visual Studio 2022 预览版创建了一个新的 ASP.NET Core 项目,并尝试将它作为 Windows 服务运行。 I downloaded the latest Microsoft.Extensions.Hosting.WindowsServices package (6.0.0-preview.7.21377.19).我下载了最新的 Microsoft.Extensions.Hosting.WindowsServices 包 (6.0.0-preview.7.21377.19)。

When researching online the function .UseWindowsService() goes into CreateHostBuilder method.在线研究时,函数 .UseWindowsService() 进入 CreateHostBuilder 方法。 But in the new template it looks different.但在新模板中,它看起来不一样了。 I cannot understand where I should call .UseWindowsService in the new template.我不明白我应该在新模板中在哪里调用 .UseWindowsService 。 This is my current code, it looks like the service is starting but then when I browse to localhost:5000 it gives me 404 error这是我当前的代码,看起来服务正在启动,但是当我浏览到 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();

I published my service exe like this我像这样发布了我的服务 exe

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

The following coding sets the lifetime to WindowsServiceLifetime and enables logging to the event log.以下代码将生命周期设置为 WindowsServiceLifetime 并启用日志记录到事件日志。 In most cases this should be all you need to run the app as a Windows Service.在大多数情况下,这应该是您将应用程序作为 Windows 服务运行所需的全部内容。

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

Since simply using由于简单地使用

builder.Host.UseWindowsService();

will not work with WebApplication.CreateBuilder() ( see ), but instead will throw the exception不适用于WebApplication.CreateBuilder()请参阅),而是会抛出异常

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.

or rather will cause this error或者更确切地说会导致这个错误

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

when trying to start the Service with Start-Service in PowerShell, I found a workaround that worked for me尝试在 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();

here: An asp.net core web api application, using "UseWindowsService", reports an error “System.NotSupportedException: The content root changed.此处: 使用“UseWindowsService”的 asp.net 核心 Web api 应用程序报告错误“System.NotSupportedException:内容根目录已更改。 Changing the host configuration is not supported ” when starting the service 启动服务时不支持更改主机配置”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM