Why does my Web App run fine in debug but not when it is a Windows Service?

37 Views Asked by At

I have a console app that I install as a Windows Service using the Topshelf extension. I thought I should give it a UI so I changed the sdk in the.csproj from

Project Sdk="Microsoft.NET.Sdk"

to

Project Sdk="Microsoft.NET.Sdk.Web"

I then copied code and folders (wwwroot, Controllers etc) from a ASPNET Core WebApp project. I added the following code to the Start method to run the WebApplication.

 var builder = WebApplication.CreateBuilder(args);

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

var app = builder.Build();

 // Configure the HTTP request pipeline.
 if (!app.Environment.IsDevelopment())
 {
     app.UseExceptionHandler("/Home/Error");
     // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
     app.UseHsts();
 }

 app.UseHttpsRedirection();
 app.UseStaticFiles();

 app.UseRouting();

 app.UseAuthorization();

 app.MapControllerRoute(
     name: "default",
     pattern: "{controller=Home}/{action=Index}/{id?}");

 app.Start(); //NOTE use of Start and not Run.  Because Run blocks.

I then ran the solution in Debug. Everything works fine. The web page launches and my old console app does it thing (which is basically pinging external websites to check my ISP provider has not dropped my line, again).

However, when I run the Topshelf command to install the exe as a Windows Service and start the service, the Console code runs but the Web part is not available on the expected URLs as set in launchSettings.json.

Is there some setting I should be looking at? Is this a thing that is possible to do?

0

There are 0 best solutions below