简体   繁体   English

修改 VS 生成的 gRPC 服务器代码以运行为 Windows 服务看起来不像示例

[英]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.

相关问题 如何从 VS 2022 创建的 gRPC 服务器创建 Windows 服务? - How create Windows Service from VS 2022 created gRPC server? "Grpc:C# 服务器作为 Windows 服务是否可能?" - Grpc: C# server as a Windows Service is it possible? ObserveOn与修改代码以在主线程上运行 - ObserveOn vs modifying code to run on main thread 长时间运行Windows服务与内存泄漏?或者看起来像一个? - Long running windows service with a memory leak? Or just looks like one? gRPC 作为 Windows 服务 - gRPC as Windows Service 如何在Visual Studio 2015和Windows 10上运行DirectX代码示例? - How to run DirectX code samples on Visual Studio 2015 and Windows 10? 修改Windows服务可执行文件的路径 - Modifying path to executable of windows service 当我运行我的 C# 应用程序时,Visual Studio 报告它已经加载了一个托管二进制文件(看起来像)一个随机生成的名称 - When I run my C# application, Visual Studio reports that it has loaded a managed binary with (what looks like) a randomly generated name 我尝试在VS中运行一些示例 - I try to run some samples in VS 不是将EventLog使用Designer生成的代码放置在Windows Service应用程序中就可以了吗? - Not EventLog dispose in Windows Service app using the Designer generated code is fine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM