【问题标题】:Blazor server app hosted in Windows serviceWindows 服务中托管的 Blazor 服务器应用程序
【发布时间】:2023-01-19 05:15:20
【问题描述】:

我们如何将 Blazor 服务器应用程序托管为 Windows 服务?使用本文作为指南:

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0

我们使用 dotnet 版本 6.0 创建了一个最小示例。首先从模板创建一个 Blazor 服务器应用程序。

dotnet new blazorserver

然后为 Microsoft.Extensions.Hosting.WindowsServices 添加 NuGet 包

dotnet add package Microsoft.Extensions.Hosting.WindowsServices

Program.cs 中,将主机配置为作为 Windows 服务运行。

//...
builder.Services.AddSingleton<WeatherForecastService>();

// Configure to run as Windows service
builder.Host.UseWindowsService();

var app = builder.Build();
//...

将应用发布为可执行文件。

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

将 /bin/Release/net6.0/win-x64/publish/ 文件夹中的内容复制到服务器。在服务器上,cd 到包含 exe 的文件夹并从命令行运行 exe。

PS C:\inetpub\wwwroot\TestBlazor> .\blazor-server-as-service.exe
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\inetpub\wwwroot\TestBlazor\
info: Microsoft.Hosting.Lifetime[0]

成功。

配置新的窗口服务。

New-service -Name "TestBlazorService" -BinaryPathName C:\inetpub\wwwroot\TestBlazor\blazor-server-as-service.exe

编辑服务以使用我的凭据。授予以服务身份登录的权限。启动服务。

PS> start-service TestBlazorService
start-service : Service 'TestBlazorService (TestBlazorService)' cannot be started due to the following error: Cannot
start service TestBlazorService on computer '.'.
At line:1 char:1
+ start-service TestBlazorService
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
   ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

从事件日志:

A timeout was reached (30000 milliseconds) while waiting for the TestBlazorService service to connect.

The TestBlazorService service failed to start due to the following error: 
The service did not respond to the start or control request in a timely fashion.

我错过了什么?

【问题讨论】:

    标签: c# windows-services blazor-server-side


    【解决方案1】:

    这花费的时间比我想承认的要长。 blazorserver 模板创建了一个WebApplicationBuilder

    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddRazorPages();
    builder.Services.AddServerSideBlazor();
    builder.Services.AddSingleton<IDependencyRepository, DependencyRepository>();
    
    var app = builder.Build();
    

    我试图访问和配置IHostBuilder成员,然后构建和使用WebBuilder

    //...
    builder.Services.AddSingleton<WeatherForecastService>();
    
    // Configure to run as Windows service
    builder.Host.UseWindowsService();
    
    var app = builder.Build();
    

    我对 asp.net 主机模型的理解不是很好,但我认为我需要创建一个 IHostBuilder 实例,将其配置为作为服务运行,并在其中为所有与 Web 相关的设置配置 IWebHostBuilder。然后实际构建 IHostBuilder - 我配置为作为服务运行的东西。

    我添加了一个 Startup 类来处理 Web 配置并将 Program.cs 更改为:

    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
        .UseWindowsService()
        .Build()
        .Run();
    

    安装在服务器上。服务成功启动并运行。也许有更好的方法。如果没有,我希望这会帮助别人。

    【讨论】:

    • link WebApplicationBuilder创建后好像无法更改主机。我认为 UseWindowsService 的方法不适用于 dotnet 6 最小托管模型。
    【解决方案2】:

    步骤 1. 安装Microsoft.Extensions.Hosting.WindowsServices来自 NuGet 包管理器。 步骤 2. 添加到 program.cs

    using Microsoft.Extensions.Hosting.WindowsServices;
    

    步骤 3. 改变var builder = WebApplication.CreateBuilder(args);到以下代码:

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

    步骤 4. 添加之前var app = builder.Build();到以下代码:

    builder.Host.UseWindowsService();
    

    步骤 5. 将应用发布到文件夹 步骤 6. 以管理员身份运行 cmd.exe。 步骤 7. 使用以下命令创建服务:

    sc create <SERVICE_NAME> binPath="<BIN_PATH>"
    

    例子:

    sc create MySerice binPath="C:MySolutionMyProjectinRelease
    et7.0publishMyProject.exe"
    

    步骤 8. 使用以下命令启动服务:

    net start <SERVICE_NAME>
    

    例子:

    net start MySerice 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2020-09-26
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多