2

We have been having an issue migrating to Azure Devops with .Net Core to deploy to our on-prem Windows servers. It's probably due to a lack of knowledge of the platform but nonetheless...

The problem we are trying to figure out is when following a "build once and deploy everywhere" structure, how do we transform our web.config file to set the "ASPNETCORE_ENVIRONMENT" env variable to the correct environment that is being deployed. Currently our apps need that variable set because we have our appsettings split between 2 files, appSettings.json and appSettings.Env.json, where Env is the current environment we are deploying to (Dev or Prod).

For example: Azure Pipelines builds our project and the resulting web.config in the output artifact has the ASPNETCORE_ENVIRONMENT variable set to "Development". That's great since we then deploy that artifact to our dev environment. But what happens when we release that to our prod stage? That web.config still has the ASPNETCORE_ENVIRONMENT variable set to "Development".

What is the proper (2021) way to transform that config? should I not even use that variable in the web.config and change up how we interact with our appSettings file?

I've read through many article and questions about similar issues but not many related to the ASPNETCORE_ENVIRONMENT env variable. Some say use the "Replace token task". Some say use config transforms (they don't work for ASPNETCORE_ENVIRONMENT).

Not sure where to go from here.

What I've recently tried:

Transform web.config for Azure Website Deployment for each release environment

Azure Pipelines Config Transform based on Stage

My generated web.config file on publish:

<?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=".\WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
    <environmentVariables>
      <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    </environmentVariables>
  </aspNetCore>
</system.webServer>

Azure Pipeline file

name: WebApp-$(Date:yyyyMMdd).$(rev:rr)

trigger:
branches:
 include:
 - master

variables:
  major: 2
  minor: 0
  patch: 0
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  Parameters.WebsiteName: 'webapp'

jobs:
- job: Build  
  pool:
    vmImage: 'windows-latest'

steps:
  - task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: 'restore'
    projects: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="webapp" /p:EnvironmentName="Development"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: ArchiveFiles@2
  displayName: 'Create Zip Archive'
  inputs:
    rootFolderOrFile: '$(build.artifactStagingDirectory)/'
    includeRootFolder: false
    archiveType: zip
    archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildNumber).zip
    replaceExistingArchive: true  

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: true

- task: PublishBuildArtifacts@1
  displayName: 'Web Artifact'
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
TheNameHobbs
  • 679
  • 7
  • 21
  • 36
  • I'm confused by what you're asking. ASP .NET Core doesn't use a web.config for configuration, it uses appsettings.json. And an environment variable and a web.config/appsettings.json are different things. If you want to set an environment variable, have your deployment pipeline set the environment variable. If you want to transform a file, follow standard practices for transforming files. – Daniel Mann Jul 08 '21 at 22:29
  • The publishing of my .Net Core WebApi still generates a web.config file. Inside that generated web.config file is the ASPNETCORE_ENVIRONMENT env variable which is used to set the environment in order to access my config settings in appsettings.Env.json. without that ASPNETCORE_ENVIRONMENT variable set in the web.config, my app does not load the appsetting.Env.json file. – TheNameHobbs Jul 08 '21 at 22:33

1 Answers1

1

As you se web.config here sets environmentVariable as this you don't need this if handle this in another way, like for instance setting this by you on a place where you want to deploy your app. More information you can find here in docs - Use multiple environments in ASP.NET Core. But if you still want to do this via web.config please check this answer.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • I ended up setting the environment variable in the Application host config in IIS. So it is no longer required or inserted by my build pipeline. – TheNameHobbs Jul 14 '21 at 23:55