1

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?

Velocedge
  • 1,222
  • 1
  • 11
  • 35

1 Answers1

1

Here is an answer that's about creating the service: How create Windows Service from VS 2022 created gRPC server?

If no ports are specified, Kestrel binds to 5000 and 5001 for HTTPS: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0#configureiconfiguration-1

Can you try without certificates on port 5000 or another one?

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0#configureiconfiguration-1

    "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    },
    "Endpoints": {
          "Http": {
            "Url": "http://localhost:50500"
           },
          }

and add

builder.Services.AddGrpcReflection();

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

Can you try if it returns a response with

grpcurl -plaintext localhost:50500 list

grpcui -plaintext localhost:50500

The client channel needs to have the same port as Kestrel.

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

DMinovski
  • 61
  • 2
  • 2
  • 7
  • 1
    LOL... that link you referenced is my post. I thought I deleted this one. Very good comments though, thanks. – Velocedge Sep 05 '22 at 10:06