0
votes

I'm trying to configure ASP.NET Core project publication profile for deployment to staging environment. There's preconfigured ASPNETCORE_ENVIRONMENT variable set on server, but whatever I try Visual Studio keeps adding ASPNETCORE_ENVIRONMENT variable definition to publishing web.config file. The only way to eliminate it is to delete from launchSettings.json.

This causes several issues:

  1. Although docs.microsoft.com says this option should completely override the environment variable, in fact the value of HostingEnvironment.EnvironmentName propery is set to "Staging;Environment", concatenated values of environment variable and web.config setting.
  2. That in consequesce prevents apropriate appsettings.{environment}.json from load and breaks all environment aware logic.
  3. I'd prefer to keep ASPNETCORE_ENVIRONMENT in launchSettings.json for debugging.

I could not find any setting that controls this behaviour. Is there any?

This is my .pubxml

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Release.Main</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <ProjectGuid>433bb565-5e85-4f1a-9dd4-a7f437fdb534</ProjectGuid>
    <SelfContained>false</SelfContained>
    <_IsPortable>true</_IsPortable>
    <MSDeployServiceURL>http://192.168.0.22</MSDeployServiceURL>
    <DeployIisAppPath>auth/main</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>RemoteAgent</MSDeployPublishMethod>
    <EnableMSDeployBackup>True</EnableMSDeployBackup>
    <_SavePWD>False</_SavePWD>
  </PropertyGroup>
</Project>

launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "https://localhost/DocShellWeb.Sts",
      "sslPort": 0
    }
  },
  "profiles": {
    "DocShellWeb.Sts": {
      "commandName": "IIS",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Deployed web.config

<?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="..." arguments="..." stdoutLogEnabled="false" hostingModel="InProcess" stdoutLogFile=".\logs\stdout">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: 706412a5-be0d-452d-a9b0-ce00d799f990-->
1
You need to add <EnvironmentName>Development</EnvironmentName> in your pubxml file.jpgrassi
I mean, add this key in your file, but with the appropriate environment. That should add an environmentVariables section in your generated Web.Configjpgrassi
@jpgrassi I'd prefer there won't be any ASPNETCORE_ENVIRONMENT at web.config, because even if it set for appropriate environment, the EnvironmentName in runtime is a result of concatenation of the evironment variable and the value in web.config separated by semicolon, e.g. Staging;Staging, Staging;Development etc.Leff
Well, that is strange then. I just created a sample app, with WebDeploy and it adds the environment correctly for me. Did you try creating a new profile and set to FileSystem, just to test things out?jpgrassi
@jpgrassi well, I mean there is preconfigured ASPNETCORE_ENVIRONMENT variable set on server, and when it also set at web.config the resulting value isn't overrided (as said in MS docs), but rather concatenated. So, the question is how can I keep it at launchSettings.json for local debugging, but eliminate it in web.config on deploymentLeff

1 Answers

2
votes

The launchSettings.json file is only used by Visual Studio during debugging and when running the app via dotnet run. It is not used at all or even considered when publishing. If you want to specify a particular environment when you publish, then you need to add add <EnvironmentName>Staging</EnvironmentName> to the pubxml file:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
      ...
      <EnvironmentName>Staging</EnvironmentName>
  </PropertyGroup>
</Project>

This will have the effect of adding an environment variable declaration for ASPNETCORE_ENVIRONMENT set to Staging in your published web.config.

However, that assumes a few things:

  1. That you're using IIS to host. Only IIS cares about a web.config file, so if you're hosting with a different web server, you'll obviously need a different strategy.
  2. That you want to actually tie the published app to a particular environment in the first place. One of the nice things about ASP.NET Core apps is that they are not built for a particular environment, such as was the case for ASP.NET apps. You can take the same published code and drop it in any environment. The environment then, can be set with an ASPNETCORE_ENVIRONMEMT environment variable on the server (i.e. set it as Staging for a staging server, and then any app published there will automatically use the Staging environment. Or, it can be set with an app setting if you're using Azure. In either case, you benefit from abstracting the environment from the actual publish code, allowing you to do a CI/CD pipeline, without having to constantly publish.