简体   繁体   中英

Azure DevOps - .NET Core Build Include Web.config file

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?

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

Azure DevOps部署任务

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM