首页 > 解决方案 > ASPNETCORE_ENVIRONMENT 未在动态生成的 Web.config 中设置

问题描述

遵循诸如this之类的SO答案,而在部署的Web.config中没有成功,并生成了以下部分:

<environmentVariables>
  <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="development" />
</environmentVariables>

这是我的发布命令:

dotnet publish Tsl.Submittal.sln -v m /p:PublishProfile="Properties\PublishProfiles\SubmittalService - development.pubxml" /p:Configuration=Debug /p:AllowUntrustedCertificate=True /p:Password=***** /p:EnvironmentName=development

这是我的发布资料:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>http://MyServer:80/WebSvcs/SubmittalService</SiteUrlToLaunchAfterPublish>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>9f9bb353-9ef3-4991-ad64-e4e9961ffa7f</ProjectGuid>
    <MSDeployServiceURL>https://MyServer:8172/msdeploy.axd</MSDeployServiceURL>
    <DeployIisAppPath>Default Website/WebSvcs/SubmittalService</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
    <EnableMSDeployBackup>True</EnableMSDeployBackup>
    <UserName>deployUser</UserName>
    <_SavePWD>True</_SavePWD>
    <TargetFramework>net472</TargetFramework>
    <RuntimeIdentifier>win7-x86</RuntimeIdentifier>
    <EnvironmentName>development</EnvironmentName>
  </PropertyGroup>
</Project>

发布和部署成功,这是当前生成的 Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\Tsl.Submittal.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>
<!--ProjectGuid: 9f9bb353-9ef3-4991-ad64-e4e9961ffa7f-->

如果我从 Visual Studio 发布,使用相同的发布配置文件 Web.config 确实具有<environmentVariable>条目:

<?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=".\Tsl.Submittal.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: 9f9bb353-9ef3-4991-ad64-e4e9961ffa7f-->

我尝试了一些变体,例如发布配置文件的显式文件路径:

dotnet publish Tsl.Submittal.sln -v m /p:PublishProfile="src/Tsl.Submittal/Properties/PublishProfiles/SubmittalService - staging.pubxml" /p:Configuration=Release /p:AllowUntrustedCertificate=True /p:Password=**** /p:EnvironmentName=staging

并且还使用启动项目而不是解决方案文件:

dotnet publish src/Tsl.Submittal/Tsl.Submittal.csproj -v m /p:PublishProfile="src/Tsl.Submittal/Properties/PublishProfiles/SubmittalService - staging.pubxml" /p:Configuration=Release /p:AllowUntrustedCertificate=True /p:Password=**** /p:EnvironmentName=staging

这是我的csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <Configurations>Debug;Release;development;staging;production</Configurations>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Any CPU'">
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Any CPU'">
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='development|Any CPU'">
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='staging|Any CPU'">
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
    <PackageReference Include="MSBuild.Microsoft.VisualStudio.Web.targets" Version="14.0.0.3" />
    <PackageReference Include="ServiceStack.Core" Version="5.4.1" />
    <PackageReference Include="ServiceStack.Admin.Core" Version="5.4.1" />
    <PackageReference Include="ServiceStack.Api.OpenApi.Core" Version="5.4.1" />
    <PackageReference Include="Tsl.Framework.Helpers" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Tsl.Submittal.Data\Tsl.Submittal.Data.csproj" />
    <ProjectReference Include="..\Tsl.Submittal.Interfaces\Tsl.Submittal.Interfaces.csproj" />
    <ProjectReference Include="..\Tsl.Submittal.Model\Tsl.Submittal.Model.csproj" />
    <ProjectReference Include="..\Tsl.Submittal.Service.Facade\Tsl.Submittal.Service.Facade.csproj" />
    <ProjectReference Include="..\Tsl.Submittal.Service\Tsl.Submittal.Service.csproj" />
  </ItemGroup>

  <ProjectExtensions><VisualStudio><UserProperties appsettings_1staging_1json__JSONSchema="" /></VisualStudio></ProjectExtensions>
</Project>

标签: asp.net-core.net-coremsbuildmsdeploy

解决方案


推荐阅读