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












1















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=".logsstdout">
<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?










share|improve this question























  • 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















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=".logsstdout">
<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?










share|improve this question























  • 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












1








1


1






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=".logsstdout">
<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?










share|improve this question














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=".logsstdout">
<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?







c# .net-core azure-devops azure-pipelines azure-pipelines-build-task






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 19:45









Kyle BarnesKyle Barnes

1161315




1161315













  • 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



















  • 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

















Any reason for not using just build variables for replacement?

– Camilo Terevinto
Nov 17 '18 at 19:52





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





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 Answer
1






active

oldest

votes


















2














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>





share|improve this answer
























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354920%2fazure-devops-net-core-build-include-web-config-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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>





share|improve this answer
























  • 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
















2














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>





share|improve this answer
























  • 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














2












2








2







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>





share|improve this answer













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>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 '18 at 21:13









Štěpán ZechnerŠtěpán Zechner

941




941













  • 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 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 does not answer the question. Appsettings.**json** files are not XML.

– Daniel Mann
Nov 17 '18 at 21:38





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







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







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





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


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354920%2fazure-devops-net-core-build-include-web-config-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini