I have a simple ASP.NET Core 3.1 running well locally. Trying to run it as a Windows Service, it starts and I am able to interact with controllers but it seems like it is unable to load the appsettings.json
file.
Here is the output of my home controller when running locally:
Hello World running locally - Configuration Minutes: 1 - Server: server-xyz - HeartBeatRunnerClass: grp.csa.soi.soicat.runners.HeartBeatRunner - Env Name: Development - Application: grp.csa.soi.ads.web - Root Path: C:\****\grp.csa.soi.ads.web!!!
Here is the output of my home controller when running in Windows Service:
Hello World running as a Windows Service - Configuration Minutes: 0 - Server: - HeartBeatRunnerClass: - Env Name: Production - Application: grp.csa.soi.ads.web - Root Path: C:\WINDOWS\TEMP\.net\grp.csa.soi.ads.web\rr4fpdsz.c2n\!!!
Here is the relevant part of my Program file:
public static void Main(string[] args)
{
var builder = CreateHostBuilder(args);
if (WindowsServiceHelpers.IsWindowsService())
{
builder.ConfigureHostConfiguration(hostBuilder =>
{
hostBuilder.SetBasePath(System.AppDomain.CurrentDomain.BaseDirectory);
});
}
builder.Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseWindowsService();
}
Notes:
- I thought by using
useWindowsService()
extension method, it is supposed to setup the content root based on this. - I have packaged my publish to place the
appsettings.json
file next toexe
file. - I have tried different combinations of app configuration based on other posts like this but nothing worked for me.
I appreciate any pointer.
Regards