【发布时间】: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