【问题标题】:Azure DevOps - .NET Core Build Include Web.config fileAzure DevOps - .NET Core 构建包含 Web.config 文件
【发布时间】:2019-04-20 15:35:00
【问题描述】:

我正在使用 Azure DevOps 构建 .NET Core MVC Web 应用程序并将其发布到 AWS 中的 Windows Server 2016 EC2 实例。

我有不同的环境,所以我创建了以下 appsettings.json 文件:

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

经过一番研究,我发现我们可以在 web.config 文件中设置 ASPNETCORE_ENVIRONMENT 变量:

<?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>

然后我可以使用 Program.cs 中的以下代码加载相应的环境 appsetting.json 文件:

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);

        }
    }

对于每个部署,我想控制所部署的 web.config,以便我可以控制 ASPNETCORE_ENVIRONMENT 的值。类似于传统 ASP.NET 环境中的 web.config 转换。

有没有办法在 Azure DevOps 中或通过 Visual Studio 中的设置来执行此操作?我已经读到 .NET Core 2.2 将为此提供解决方案,但在此期间可以做些什么呢?

【问题讨论】:

  • 有什么理由不只使用构建变量进行替换?
  • 能否不直接在服务器上设置环境变量,这就是使用环境变量来保存该信息的全部意义所在。如果看不到@CamiloTerevinto 的评论,那会容易得多。

标签: c# .net-core azure-devops azure-pipelines azure-pipelines-build-task


【解决方案1】:

我正在使用标准的 web.config 转换(部署到 IIS)

改造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>

【讨论】:

  • 这没有回答问题。 Appsettings.**json** 文件不是 XML。
  • 这可能很有用。那么,您是否将 web.config 文件添加到您的 .NET Core 项目中?那么您是否根据需要包含不同的环境?如果是这样,这似乎对我的情况有用。在 Azure DevOps 中的哪里设置 EnvironmentName?
  • 是的,我有 web.config、web.*.config 和 appsettings.json、appsettings.*.json。环境名称是任务中的“阶段名称”..
  • 不是一个理想的方案,但这种方法使我能够部署到不同的环境。希望他们对 .NET Core 2.2 有更好的方法。
  • 这个解决方案在没有 标签的情况下对我有用
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 2022-07-14
  • 1970-01-01
相关资源
最近更新 更多