1

I have a .NET Core worker service app, I have different appsetings.json files like appsetings.Dev.json, appsetings.Prod.json etc. I want to load these appsettings file based on the environment variables provided via either Publish Profiles or command line publishes.

For example, in ASP.NET Core apps, I can add the following tag into .pubxml file

<PropertyGroup>
    <EnvironmentName>Dev</EnvironmentName>
</PropertyGroup>

For, ASP.NET Core web apps this will automatically set the environment as Dev and the release build will load the appsetings.Dev.json file.

For the background app, this approach doesn't work. How can I set environment variables for .net 6 worker apps?

I've also tried to supply environment variables via CLI, but the release build app always uses the environment as "Production" no matter what is provided (Staging, Development, QA etc.)

Sultan
  • 23
  • 2
  • 9
  • Seems like this isn't supported yet. Discussion on similar issues can be found here: https://github.com/dotnet/runtime/issues/71104 – Sultan Aug 10 '23 at 07:06

2 Answers2

0

There are a few different ways to add env variables to your project

  1. Can use dotnet run command, and pass env variables as parameter using -p flag. For example dotnet run -p:Environment=Development -p:EnvVar=value1
  2. Using configuration files like appsettings.json to store values of env variables and read them at runtime. You can use Microsoft.Extensions.Configuration library for it
  3. Set ENV variables from code using this method Environment.SetEnvironmentVariable("EnvVar", "secretkey");

Environment variables set with the dotnet run command are only available during the execution of the command and they are not set globally in the system. On the other hand, by setting them through the Environment class or by modifying the environment in the machine or in the server, these variables will be available for all the applications running in the system/machine.

Gor Grigoryan
  • 297
  • 1
  • 7
  • 1
    None of your approaches answers my question. **1.** `dotnet run -p:Environment=Development -p:EnvVar=value1` - this for running the app in local machine, not using a release build, right? **2.** Using configuration files like appsettings.json to store values of env variables and read them at runtime. You can use Microsoft.Extensions.Configuration library for it - if I store the environment variable in `appsettings.json` file how does that work to read the file `appsettings.{EnvironmentName}.json`? – Sultan Jan 20 '23 at 07:32
  • **3.** Set ENV variables from code using this method - that is a hardcoded way to supply environment variables. Every time I have to change the code for a different release build (Staging, QA, Dev etc), right? – Sultan Jan 20 '23 at 07:36
  • " by modifying the environment in the machine or in the server, these variables will be available for all the applications running in the system/machine." - why would you set the application-specific environment variable globally? – Sultan Jan 20 '23 at 07:39
  • 1. If you are using pipelines then for each stage you can have parameters and use those variable parameters and pass them to run stage like this - task: AzureRmWebAppDeployment@4 displayName: 'Deploy' inputs: AppSettings: '-variable1 ${{ parameters.variable1 }} -variable2 ${{ parameters.variable1 }}' ExcludeFilesFromAppDataFlag: false – Gor Grigoryan Jan 20 '23 at 08:56
  • my background app is registered as a Windows Service on the server, I'm not running it from a pipeline. There should be some way to provide environment variables while creating a release build, so the release build app can access the environment variables – Sultan Jan 20 '23 at 10:29
  • Then have you tried to use https://github.com/bolorundurowb/dotenv.net ? With this package, you can create .env field for all your environments, and then get them – Gor Grigoryan Jan 20 '23 at 13:59
  • 1
    No I haven't used any third-party package, there should be an inbuilt functionality to provide and access environment variables for console/worker apps – Sultan Jan 26 '23 at 11:17
0

Configure your .csproj file with below lines

  <PropertyGroup Condition="'$(Configuration)'=='Release'">
    <EnvironmentName>Production</EnvironmentName>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)'=='QA'">
    <EnvironmentName>Quality</EnvironmentName>
  </PropertyGroup>
 <PropertyGroup Condition="'$(Configuration)'=='Dev'">
    <EnvironmentName>development</EnvironmentName>
 </PropertyGroup>

above lines checks your configuration name if that configuration is Release then your environment name will be Production,if that configuration is QA then your environment name will be Quality,if that configuration is Release then your environment name will be development in Web.config After publish in published folder.

Configure your Program.cs file as below

    builder.Configuration
      .SetBasePath(Directory.GetCurrentDirectory())
      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
      .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
      .AddEnvironmentVariables()
      .Build();

if it is Production environment consume from appsettings.Production.json if Particular environment not found uses the appsettings.json

Upender Reddy
  • 568
  • 3
  • 8
  • Your solution strictly ties to the environment as **Production** for a Release build. How do you set other environment variables (Staging, QA, Dev etc) for the release build? – Sultan Jan 20 '23 at 07:23
  • @Sultan While Publishing Check your configuration write logic for different in .csproj file. after publish check your web.config file in published folder set environment variable based on configuration – Upender Reddy Feb 06 '23 at 08:43
  • Does the `web.config` file has any impact on the console app hosted as a windows service? as far as I know, `web.config` file is required for IIS-hosted applications – Sultan Feb 06 '23 at 11:36
  • yes. It check's for key value pair's while application is starting. in this it checks for `` – Upender Reddy Feb 07 '23 at 12:02
  • I think you didn't understand my question. Does a windows service app read the environment from `web.config` file? – Sultan Feb 08 '23 at 11:26