简体   繁体   中英

Self-hosted gRPC Windows Service not listening when running as service, works stand-alone

Using VS 2022, I created a gRPC server that I intend to run as a Windows Service. The server works fine when I run it from Visual Studio or the command line. However, when I run it as a Windows Service I can't connect to it with the client. Running the utility program tcpview, I can see that is not listening on port 6276 (I can see that port when running from VS). Here is the Program.cs

var options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

builder.Services.AddGrpc();

builder.Host
    .UseWindowsService(options =>
    {
        options.ServiceName = "CADE eTutor Service Core";
    });

builder.WebHost.UseKestrel(kestrel =>
{
    kestrel.ConfigureHttpsDefaults(https =>
    {
        https.ServerCertificate = new X509Certificate2(@"D:\Data\CADE.core\LDNcert.pfx", "pw");
    });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.MapGrpcService<eTutorServiceMain>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();

launchsettings.json looks like:

{
  "profiles": {
    "eTutorService": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://srdev.learn.net:6276",
      "dotnetRunMessages": true
    }
  }
}

appsettings.json looks like:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Https": {
    "Url": "https://srdev.learn.net:6276",
    "Certificate": {
      "Path": "D:\\Data\\CADE.core\\LDNcert.pfx",
      "Password": "pw"
    }
  },
  "Certificates": {
    "Default": {
      "Path": "D:\\Data\\CADE.core\\LDNcert.pfx",
      "Password": "pw"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }
}

I've tried this with and without the Https and Certificates nodes with no change. They all work running the program from VS but none work when running as a service.

I use a wildcard certificate for *.learn.net which is installed on this computer.

I do NOT get errors in the event logs on the "server" so there is not much in the way of clues.

EDIT: On the client side, I get the following error:

DebugException="System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.

What do I need to add to make this work?

Can you try without certificates?

    "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }

and in Program.cs add

builder.UseWindowsService();

services.AddGrpcReflection();

Publish it and create a Windows service with

sc.exe create "GrpcService" binpath="C:\GrpcPublish\Project.Grpc.exe"

It seems that by default it runs on port 5000. Can you try if it returns a response with

grpcurl -plaintext localhost:5000 list

or if it can open a browser tab with

grpcui -plaintext localhost:5000

Maybe the client channel will need to have the same port

GrpcChannel.ForAddress("http://localhost:5000");

Running the commands on port 5000 maybe won't return a response while hosted from Visual Studio as it can use the same port, but running them while it's hosted as a Windows service should.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM