1

Here's my "ENTIRE" code in program.cs.

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseWindowsService();

// Add services to the container.
builder.Services.AddGrpc();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGet("/", () => "error");

app.Run();

Packages:

  • Grpc.AspNetCore(2.47.0)
  • Microsoft.Extensions.Hosting.WindowsServices (6.0.0)

I deployed this a test server and tried to start the service...and I got this in the Event Log:

Application: PartReservationGrpc.exe CoreCLR Version: 6.0.622.26707 .NET Version: 6.0.6 Description: The process was terminated due to an unhandled exception. Exception Info: System.NotSupportedException: The content root changed from "C:\windows\system32" to "D:\CMAP\iceman\partreservation". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead. at Microsoft.AspNetCore.Builder.ConfigureHostBuilder.ConfigureHostConfiguration(Action1 configureDelegate) at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.UseContentRoot(IHostBuilder hostBuilder, String contentRoot) at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder, Action1 configure) at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder) at Program.$(String[] args) in C:\Users\WuJ22\source\Workspaces\C5CG2133PXF\gRPC\PartReservation\PartReservation\PartReservationGrpc\Program.cs:line 33

line 33: is exactly where the UseWindowsService() is made.

Has anyone run into this issue?

Johnny Wu
  • 1,297
  • 15
  • 31
  • Was not able to reproduce. Can you please add a [mre]? – Guru Stron Jul 11 '22 at 14:05
  • @GuruStron...I just posted my entire program.cs (I removed all the other code to isolate the problem)...90+% was out of the box from VS2022...all I did was adding the UseWindowsService call. – Johnny Wu Jul 11 '22 at 14:47
  • How do you deploy it? – Guru Stron Jul 11 '22 at 14:55
  • right now, I am using the "sc" command to install the service on my desktop and point Release folder local on my desktop. BTW, the executable works fine if I run it through cmd. Just doesn't like it when I try to run it as a Service – Johnny Wu Jul 11 '22 at 14:58
  • @GuruStron...I just looked at the doc....UseWindowsService tries to set content root...the way it set the contentroot must not be compatible with 6.0. Since you got it to work...would u mind, sharing me your package info and/or the code? – Johnny Wu Jul 11 '22 at 15:15
  • Note that you can always manually switch back to the generic hosting model (the one with `Startup` and so on). One way is to create .NET 5 solution and change target framework to .net6.0. Or just manually copy the setup code (`Program.cs` and `Startup`) – Guru Stron Jul 11 '22 at 15:16

1 Answers1

0

Answer can be found here:

The trick is calling the CreateBuilder() with the WebApplicationOptions to set the ContentRootPath.

Johnny Wu
  • 1,297
  • 15
  • 31