Set base directory of EXE when running as Windows Service
Great tutorial, however, you need to add the below line in Main() method in order to set the working directory when running the EXE as a Windows service. Not adding will cause the Windows Service to fail since it will run under defult location( C:\Windows\System32) :
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
I came across something similar and after looking through the docs I found this example:
var isService = !(Debugger.IsAttached || args.Contains("--console"));
if (isService)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);
}
There's also a new package in .net core 3.0 called Microsoft.Extensions.Hosting.WindowsServices that handles most of this for you.
If you're using a single file exe and keeping the appsettings.json files alongside the exe then you need to call builder.UseContentRoot(pathToContentRoot) with the path of your exe (I'm using the code above for that).