ASP.Net Core 2.1 application cannot find appsettings.json when ran as a Windows service

As App configuration, we need to call SetCurrentDirectory and use a path to the app's published location.

For your issue, you access Directory.GetCurrentDirectory() before calling Directory.SetCurrentDirectory(pathToContentRoot); as you call ConfigureSerilog(); first.

Try to change the order like

    // Set up to run as a service if not in Debug mode or if a command line argument is not --console
    var isService = !(Debugger.IsAttached || args.Contains("--console"));
    if (isService)
    {
        var processModule = Process.GetCurrentProcess().MainModule;
        if (processModule != null)
        {
            var pathToExe = processModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe);
            Directory.SetCurrentDirectory(pathToContentRoot);
        }
    }
    ConfigureSerilog();

Try this.

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

If you're using Task Scheduler, you can write the directory you want into

Action Tab --> Edit --> Start in

So the program will start running from that directory. enter image description here