[英]Blazor server app hosted in Windows service
How can we host a Blazor server application as a Windows service?我们如何将 Blazor 服务器应用程序托管为 Windows 服务? Using this article as a guide:
使用本文作为指南:
https://learn.microsoft.com/en-us/as.net/core/host-and-deploy/windows-service?view=as.netcore-6.0 https://learn.microsoft.com/en-us/as.net/core/host-and-deploy/windows-service?view=as.netcore-6.0
We create a minimal example using do.net version 6.0.我们使用 do.net 版本 6.0 创建了一个最小示例。 First create a blazor server application from template.
首先从模板创建一个 blazor 服务器应用程序。
do.net new blazorserver
Then add NuGet package for Microsoft.Extensions.Hosting.WindowsServices然后为 Microsoft.Extensions.Hosting.WindowsServices 添加 NuGet package
do.net add package Microsoft.Extensions.Hosting.WindowsServices
In Program.cs
, configure the host to run as a Windows service.在
Program.cs
中,将主机配置为作为 Windows 服务运行。
//...
builder.Services.AddSingleton<WeatherForecastService>();
// Configure to run as Windows service
builder.Host.UseWindowsService();
var app = builder.Build();
//...
Publish the app as an executable.将应用发布为可执行文件。
do.net publish -c Release -r win-x64 --self-contained false
Copy contents from /bin/Release.net6.0/win-x64/publish/ folder to server.将 /bin/Release.net6.0/win-x64/publish/ 文件夹中的内容复制到服务器。 On the server, cd to the folder with the exe and run the exe from the command line.
在服务器上,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]
Success.成功。
Configure new windows service.配置新的 windows 服务。
New-service -Name "TestBlazorService" -BinaryPathName C:\.netpub\wwwroot\TestBlazor\blazor-server-as-service.exe
Edit the service to use my credentials.编辑服务以使用我的凭据。 Grant log on as service rights.
授予以服务身份登录的权限。 Start the service.
启动服务。
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
From the event log:从事件日志:
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.
What am I missing?我错过了什么?
This took way longer than I'd like to admit.这花费的时间比我想承认的要长。 The blazorserver template created a
WebApplicationBuilder
. 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();
And I was trying to access and configure the IHostBuilder
member, and then build and use the WebBuilder
.我试图访问和配置
IHostBuilder
成员,然后构建和使用WebBuilder
。
//...
builder.Services.AddSingleton<WeatherForecastService>();
// Configure to run as Windows service
builder.Host.UseWindowsService();
var app = builder.Build();
My understanding of the asp.net host model is not great, but I think I need to create an instance of IHostBuilder
, configure it to run as a services, and configure the IWebHostBuilder
within it for all the web related settings.我对 asp.net 主机 model 的理解不是很好,但我认为我需要创建一个
IHostBuilder
实例,将其配置为作为服务运行,并在其中为所有 web 相关设置配置IWebHostBuilder
。 Then actually build the IHostBuilder
- the thing I configured to run as a service.然后实际构建
IHostBuilder
- 我配置为作为服务运行的东西。
I added a Startup
class to handle the web configuration and changed Program.cs
to:我添加了
Startup
class 来处理 web 配置并将Program.cs
更改为:
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
.UseWindowsService()
.Build()
.Run();
Installed on server.安装在服务器上。 Service successfully starts and runs.
服务成功启动并运行。 Maybe there's a better way.
也许有更好的方法。 If not, I hope this will help someone else.
如果没有,我希望这会帮助别人。
Step 1. Install Microsoft.Extensions.Hosting.WindowsServices from NuGet Package Manager.步骤 1. 从 NuGet Package Manager 安装Microsoft.Extensions.Hosting.WindowsServices 。 Step 2. Add to program.cs
步骤 2. 添加到 program.cs
using Microsoft.Extensions.Hosting.WindowsServices;
Step 3. Change var builder = WebApplication.CreateBuilder(args);步骤 3. 更改var builder = WebApplication.CreateBuilder(args); to the following code:
到以下代码:
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default,
Args = args
});
Step 4. Add before var app = builder.Build(); Step 4. 在var app = builder.Build();之前添加to the following code:
到以下代码:
builder.Host.UseWindowsService();
Step 5. Publish the app to folder Step 6. Run cmd.exe as administrator.步骤 5. 将应用程序发布到文件夹 步骤 6. 以管理员身份运行 cmd.exe。 Step 7. Create a Service with the following command:
步骤 7. 使用以下命令创建服务:
sc create <SERVICE_NAME> binPath="<BIN_PATH>"
Example:例子:
sc create MySerice binPath="C:\MySolution\MyProject\bin\Release\net7.0\publish\MyProject.exe"
Step 8. Start the Service with the following command:步骤 8. 使用以下命令启动服务:
net start <SERVICE_NAME>
Example:例子:
net start MySerice
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.