[英]Modifying VS generated gRPC server code to run as Windows Service looks nothing like samples
I'm using Visual Studio 2022 to create a gRPC server and would like to turn it into a Windows Service.我正在使用 Visual Studio 2022 创建 gRPC 服务器,并希望将其变成 Windows 服务。 I've created and tested the server but when I look at the examples on the web, none of them look like the program.cs created by VS shown here:
我已经创建并测试了服务器,但是当我查看 web 上的示例时,它们都不像此处所示的 VS 创建的 program.cs:
using eTutorService.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
var app = builder.Build();
app.MapGrpcService<eTutorServiceMain>();
app.MapGet("/", () => "Communication with gRPC must be made through a gRPC client...");
app.Run();
Everything I find adds "UseWindowsService" to CreateDefaultBuilder like the following:我发现的所有内容都将“UseWindowsService”添加到 CreateDefaultBuilder,如下所示:
Host.CreateDefaultBuilder(args)
.UseWindowsService()
...
or或者
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService() // Enable running as a Windows service
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
But I can't find how the "CreateHostBuilder" stuff relates to the "WebApplication.CreateBuilder" code created by VS.但我找不到“CreateHostBuilder”的东西与 VS 创建的“WebApplication.CreateBuilder”代码有何关系。 CreateBuilder doesn't support.UseWindowsService and WebApplication doesn't support CreateDefaultBuilder.
CreateBuilder 不支持。UseWindowsService 和 WebApplication 不支持 CreateDefaultBuilder。
EDIT: I was able to add UseWindowsService like this:编辑:我能够像这样添加 UseWindowsService :
builder.Services.AddGrpc();
builder.Host
.UseWindowsService(options =>
{
options.ServiceName = "CADE eTutor Core Service";
});
It compiles and I added it to the Windows Services with that ServiceName using SC at the command line, but it fails to start with error 1053, "The service did not respond to the start or control request in a timely fashion".它编译,我将它添加到 Windows 服务中,该服务名称在命令行中使用 SC,但它无法启动并出现错误 1053,“服务没有及时响应启动或控制请求”。
Looking in the event log I see this error referencing the UseWindowService line.查看事件日志,我看到这个错误引用了 UseWindowService 行。
Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
So I'm basically lost as to how to take the gRPC template created by VS and turn it into a Windows Service.所以我基本上不知道如何将 VS 创建的 gRPC 模板转化为 Windows 服务。
Well, apparently Microsoft has changed how services are created, https://github.com/dotnet/AspNetCore.Docs/issues/23387 and I found the fix there.好吧,显然微软已经改变了服务的创建方式, https://github.com/dotnet/AspNetCore.Docs/issues/23387我在那里找到了修复。 I can't say I really appreciate why this is necessary but the following code worked as far as allowing the program to run as a Windows Service:
我不能说我真的很感激为什么这是必要的,但是下面的代码可以让程序作为 Windows 服务运行:
var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(options);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.