gRPC server running as Windows Service works stand-alone not when running as service

Steve Ricketts 6 Reputation points
2022-08-09T12:58:27.603+00:00

I created a basic gRPC Server using VS 2022 and want to run it as a Windows Service. The program runs fine in VS or Command line but when it's run as a service, it doesn't listen to the specified ports (6276). netstat doesn't show the port when it's a service but does otherwise. I'm running the service under an administrator account.

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

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

What am I missing?

Developer technologies | ASP.NET | ASP.NET Core
Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Limitless Technology 39,926 Reputation points
    2022-08-10T07:27:24.61+00:00

    Hi there,

    Try changing the default server port and see if that helps.

    To change the grpc server port from the default (9090) to a different port you can do so using:

    grpc.server.port=80

    I hope this information helps. If you have any questions please let me know and I will be glad to help you out.

    -------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  2. Steve Ricketts 6 Reputation points
    2022-08-10T12:55:17.58+00:00

    As you can see, I set the port in launchsettings.json in the .Net Core server with "applicationUrl": "https://srdev.learn.net:6276". I changed the port from 6276 but it didn't make any difference. I think the default port for VS created grpc is 5001. I don't know how to set the port the way you've suggested. Where would I put grpc.server.port=6276?

    0 comments No comments

  3. Steve Ricketts 6 Reputation points
    2022-08-14T10:38:38.617+00:00

    Nobody has created a Windows Service using VS 2022 from a gRPC server template? I just need to know how you did it.

    0 comments No comments

  4. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2022-08-16T19:10:51.77+00:00

    launch setting are only used by visual studio to start the app. app settings.json is used when standalone. be sure the app settings in the folder specified in sc.exe


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.