4

I'm using Azure DevOps for building and releasing a .NET Core MVC web application to a Windows Server 2016 EC2 instance in AWS.

I have different environments, so I've created the following appsettings.json files:

  • appsettings.DEV.json
  • appsettings.STG.json
  • appsettings.PRD.json

After some research, I see that we can set the ASPNETCORE_ENVIRONMENT variable in the web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\www.MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="[ENV]" />
      </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

I can then load the respective environment appsetting.json file using the following code in Program.cs:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(ConfigConfiguration)
                .UseStartup<Startup>();

        static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config)
        {
            config.SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);

        }
    }

For each deployment, I would like to control the web.config that gets deployed so that I can control the value of ASPNETCORE_ENVIRONMENT. Similar to web.config transform in a traditional ASP.NET environment.

Is there a way to do this in Azure DevOps, or via a setting in Visual Studio? I've read that .NET Core 2.2 will offer a solution for this, but what can be done in the meantime?

Kyle Barnes
  • 729
  • 3
  • 13
  • 22
  • Any reason for not using just build variables for replacement? – Camilo Terevinto Nov 17 '18 at 19:52
  • Can you not set the Environment Variable directly on the servers, that is the entire point of using Environment Variables to hold that information. Failing that see @CamiloTerevinto's comment as that would much easier. – Adam Carr Nov 17 '18 at 21:28

1 Answers1

4

I'm using standard web.config transformations (deploy to IIS)

Azure DevOps deploy task

Transformation web.staging.config:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables>
          <environmentVariable xdt:Transform="Replace" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Staging" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
  • This does not answer the question. `Appsettings.**json**` files are not XML. – Daniel Mann Nov 17 '18 at 21:38
  • This might be useful. So do you add a web.config file to your .NET Core Project? Then do you include different environments as needed? If so this seems this might work for my case. Where do you set the EnvironmentName in Azure DevOps? – Kyle Barnes Nov 17 '18 at 21:45
  • Yes, i have web.config, web.*.config and appsettings.json, appsettings.*.json included. Name of environment is "Stage name" in task.. – Štěpán Zechner Nov 17 '18 at 21:48
  • Not an ideal scenario, but this approach enabled me to deploy for separate environments. Hopefully, they have a better approach for .NET Core 2.2. – Kyle Barnes Nov 17 '18 at 23:59
  • This solution worked for me without the tag – Balage Sep 09 '19 at 15:37