Jon*_*Jon 3 c# windows-services asp.net-core .net-6.0
当我尝试将 .Net Web 应用程序作为 Windows 服务运行时,它无法启动。如果我只运行可执行文件,它会起作用,但在尝试作为服务运行时会神秘地失败。我已记录相关错误消息并在下面报告了我的解决方案。
事件查看器中出现以下异常。
应用程序:WebApplication5.exe CoreCLR 版本:6.0.222.6406 .NET 版本:6.0.2 说明:由于未处理的异常,进程被终止。异常信息:System.NotSupportedException:内容根目录从“C:\Windows\system32”更改为“C:\Program Files (x86)\My Company\webapp5”。不支持使用 WebApplicationBuilder.Host 更改主机配置。请改用 WebApplication.CreateBuilder(WebApplicationOptions)。在 Microsoft.AspNetCore.Builder.ConfigureHostBuilder.ConfigureHostConfiguration(操作
1 configureDelegate) at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.UseContentRoot(IHostBuilder hostBuilder, String contentRoot) at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder, Action1 配置) 在 Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder) 在 Program.$(String[] args) 在 C:\Users\Jonathan\source\repos \WebApplication5\WebApplication5\Program.cs:第 10 行
我的应用程序基于ASP.NET Core Web API模板。我按照https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio#app-configuration上的说明进行操作并添加UseWindowsService()到Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Host.UseWindowsService();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
我编译一个二进制文件dotnet publish -c Debug --self-contained --runtime win-x64 -p:PublishSingleFile=true
将输出复制到目标计算机并在管理 Powershell 中运行
New-Service -Name WebApplication5 -BinaryPathName C:\Users\WDAGUtilityAccount\Desktop\webapp5\WebApplication5.exe
Run Code Online (Sandbox Code Playgroud)
PS C:> Start-Service WebApp5
Start-Service : Service 'WebApp5 (WebApp5)' cannot be started due to the following error: Cannot start service WebApp5 on computer '.'.
At line:1 char:1
+ Start-Service WebApp5
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
Run Code Online (Sandbox Code Playgroud)
当我使用服务 GUI 时:
Windows 无法在本地计算机上启动 WebApplication5 服务
错误1053:服务没有及时响应启动或控制请求。
就像事件查看器中的异常所示,我需要使用非默认构造函数Program.cs并在那里设置ContentRootPath。
更改此默认构造函数:
var builder = WebApplication.CreateBuilder(args);
Run Code Online (Sandbox Code Playgroud)
改成:
var webApplicationOptions = new WebApplicationOptions
{
ContentRootPath = AppContext.BaseDirectory,
Args = args,
};
var builder = WebApplication.CreateBuilder(webApplicationOptions);
Run Code Online (Sandbox Code Playgroud)
这是我的其余部分program.cs供参考:
//set ContentRootPath so that builder.Host.UseWindowsService() doesn't crash when running as a service
var webApplicationOptions = new WebApplicationOptions
{
ContentRootPath = AppContext.BaseDirectory,
Args = args,
};
var builder = WebApplication.CreateBuilder(webApplicationOptions);
//All the following code is unchanged from my original program.cs
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Host.UseWindowsService();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16275 次 |
| 最近记录: |