AppSettings Environment Configuration for DotNetCore and ASPDotNetCore

Recently I had to work in a worker process project and after deployment, something wasn’t right. After spending hours, realized that the application was not loading the right configuration files. So this article is a self note on how to configure app to read from the appsettings in a dot net core environment.

Following Microsoft article is a good starting point. This would provide a general idea about hosting in dot net core

.NET Generic Host in ASP.NET Core | Microsoft Docs

From the article

Now the above snapshot works for web apps, but when it comes to worker process, that GetCurrentDirectory() will default to System32 folder and not the application folder. Hence the importance of using UseWindowsService() as explained in the next article below.

Host ASP.NET Core in a Windows Service | Microsoft Docs

Things to note when we use UseWindowsService()

  1. GetCurrentDirectory() gets you System32 folder which is not what we want.
  2. Use ContentRootPath to set the directory path explicitly, but by default its set to the AppContext.BaseDirectory which is our app location
  3. There is no need to explicitly set the SetBasePath() since the app directory would be set by default, and thus any appSettings file will be loaded
  4. Now comes the section where we should set the Environment variables in the server. When we run from Visual Studio locally it finds the Environment name set in the project properties > Debug. To set the same in the server , type the below code in command prompt.
  5. Worker process or console apps are prefixed with DOTNET and web apps are prefixed with ASPNETCORE
  6. setx DOTNET_ENVIRONMENT “Development” /M
  7. The above command would create a Environment variable in the server and in our App configuration in the Startup when we add the command config.AddEnvironmentVariables() , it would add default variables which are prefixed with DOTNET for worker process.
  8. We can have the app add other environment variables with specific prefixes by setting it as config.AddEnvironmentVariables(prefix: “PREFIX_”);
Final configuration for a worker process

This is the final version I had. We don’t need to specify SetBasePath, but I am doing it. Also remember that AddEnvironmentVariables() is last , so it would overwrite final env variable even from the json config. So make sure you have right system environment variable setup in the remote server.

And make sure to disable Copy To Output field for other environment AppSettings file, since we don’t want Dev Server having config files pointing to Production. Even though it won’t be picked, remember that default Hosting Environment is Production, so if the variable wasn’t set by any chance, the app would be using Production config files, if its present in the dev server. It would be a manual process to change it to Copy before publishing to Production.

Even with this configuration, my log files are still generated in the System32 folder by deafult. This happens only when you run the app as a windows service. Solution was to set the logfile path the in the Serilog configuration using the command below. AppContext.BaseDirectory.ToString() gives you the app directory path.

string path= Path.Combine(AppContext.BaseDirectory.ToString(), “folder”, @”filename-.txt”);

Leave a comment

Your email address will not be published. Required fields are marked *