azure-devops

How to substitute appsettings environment using Azure Devops classic pipeline?


In a .NET 8 project, I am trying to substitute appsettings.development.json for development and appsettings.production.json for production, however the production json is always being chosen. I'm using Azure Devops and have a classic build pipeline where I'm setting the ASPNETCORE_ENVIRONMENT variable in a pipeline variable to 'development'.

Here's the code from my program.cs file. When I run the build and release, the site is using appsettings.production.json on my development environment.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;

            config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
                  .AddEnvironmentVariables();
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Solution

  • From your description, you are defining the variable: ASPNETCORE_ENVIRONMENT in Pipeline. In this case, this variable is only available in the pipeline.

    After you deploy the project, the hosting site does not set this variable, so it will use the Production environment by default.

    To solve this issue, you can set the environment variable when running dotnet publish.

    For example:

    dotnet publish -c release /p:EnvironmentName=Development
    

    Pipeline task:

    - task: DotNetCoreCLI@2
      displayName: Publish
      inputs:
        command: publish
        publishWebProjects: True
        arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)  /p:EnvironmentName=Development'
        zipAfterPublish: True
    

    In the output package, we can find environment variable: ASPNETCORE_ENVIRONMENT in the web.config file.

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
          </handlers>
          <aspNetCore processPath="dotnet" arguments=".\EnvironmentsSample.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
            <environmentVariables>
              <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
            </environmentVariables>
          </aspNetCore>
        </system.webServer>
      </location>
    </configuration>
    

    After deployment, it will apply the correct appsettings file.

    On the other hand, you can also manually set the global environment variable(e.g. ASPNETCORE_ENVIRONMENT:development) in the hosting site.

    If you are using Azure Web App, you can set the required variable in App setting -> Add/Edit application setting.

    If you are using IIS web site, you can set the variable in IIS -> Configuration Editor -> system.webServer/aspNetCore -> environmentvariables.

    For more detailed info, you can refer to this doc: Use multiple environments in ASP.NET Core