【问题标题】:Config Transforms not applying on Web API publish配置转换不适用于 Web API 发布
【发布时间】:2013-10-15 05:45:33
【问题描述】:

我有一个 ASP.NET Web API 项目,其中包括 2 个配置转换:

  • Web.Live.config
  • Web.UAT.config

如果我在发布时选择LiveUAT 配置,则转换不会应用于呈现的web.config 文件。

我检查了我的转换配置,namexdt:Transformxdt:Locator 是正确的。

在我的web.config 我有:

<connectionStrings>
    <add name="foo" providerName="System.Data.SqlClient" connectionString="[main connection string]" />
</connectionStrings>

在我的web.Live.config 我有:

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="foo" 
      connectionString="[live connection string]" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

但是,即使选择了Live,我发布的连接字符串仍然显示如下:

<connectionStrings>
    <add name="foo" providerName="System.Data.SqlClient" connectionString="[main connection string]" />
</connectionStrings>

发生这种情况的可能原因有哪些?

【问题讨论】:

  • 你发表的怎么样了?您使用的是“发布配置文件”吗?根据您发布的“连接”类型(例如 Web 部署),您将获得特定的“设置”,允许您设置连接字符串。我不确定,但我想知道这是否设置为您的主连接字符串并且优先于您的配置转换?至少可能值得一看(取消选中“在运行时使用此连接字符串”)。除此之外,配置文件本身看起来还可以
  • @musefan 谢谢,我尝试在appSettings 上执行xdt:Transform="Replace",这些更改已应用于渲染配置!因此,这绝对是连接字符串部分的问题。但是,当我在 VS2012 中“预览变换”时,它确实应用了变换
  • 呵呵,不知道你可以预览变换,太酷了。无论如何,你是如何进行发布的?如果预览有效,那么它一定是在转换后覆盖它的其他东西
  • 啊,我不认为“文件系统”有这个选项。只有“Web Deploy”和“Web Deploy Package”似乎……回到了思考板!
  • @musefan 解决了!我对这两种配置类型都使用了 1 配置文件。我已对此进行了更改,因此每种配置类型都有自己的配置文件,现在可以正常工作。请随时发布答案,以便我标记它!干杯。 P.S:缪斯太棒了!

标签: asp.net configuration asp.net-web-api publish web-config-transform


【解决方案1】:

似乎这种情况下的问题是假设更改构建配置文件会自动更新您的发布配置文件以匹配 - 事实并非如此。

如果您有一个发布配置文件,那么您必须在每次构建时手动更改“设置”>“发布”>“配置”设置,因为这是决定应用哪个配置转换文件的原因。

处理此问题的推荐方法是创建多个发布配置文件 - 每个构建配置一个。例如,“Debug”和“Release”,或者在本例中为“Live”和“UAT”。这样您就可以在发布过程中轻松地在两个配置文件之间切换。

值得注意的是,这种方法除了管理转换之外还有其他好处。为不同的构建提供不同的配置文件也允许您指定不同的目标。例如,如果您使用 Web Deploy 方法,您可以根据您的目标环境(例如测试或生产)将应用程序发送到不同的服务器

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,应用的转换将来自“调试”,而不是来自我的“测试”配置。我通过手动编辑一些文件来解决它。我不确定哪些是错的,以及它们是如何出错的,但这就是我解决它的方法。

    我将 .sln 从“调试”更改为:

        {96...D2A}.Test|Any CPU.ActiveCfg = Debug|Any CPU
        {96...D2A}.Test|Any CPU.Build.0 = Debug|Any CPU
        {96...D2A}.Test|Mixed Platforms.ActiveCfg = Debug|Any CPU
        {96...D2A}.Test|Mixed Platforms.Build.0 = Debug|Any CPU
        {96...D2A}.Test|x86.ActiveCfg = Debug|Any CPU
    

    到“测试”:

        {96...D2A}.Test|Any CPU.ActiveCfg = Test|Any CPU
        {96...D2A}.Test|Any CPU.Build.0 = Test|Any CPU
        {96...D2A}.Test|Mixed Platforms.ActiveCfg = Test|Any CPU
        {96...D2A}.Test|Mixed Platforms.Build.0 = Test|Any CPU
        {96...D2A}.Test|x86.ActiveCfg = Test|Any CPU
    

    我将 .csproj 中的测试配置更改为:

    <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|AnyCPU'">
      <DebugSymbols>true</DebugSymbols>
      <OutputPath>bin\</OutputPath>
      <DefineConstants>DEBUG;TRACE</DefineConstants>
      <DebugType>full</DebugType>
      <PlatformTarget>AnyCPU</PlatformTarget>
      <ErrorReport>prompt</ErrorReport>
      <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
    

    to(我不确定所有这些删除的参数是什么意思/暗示):

    <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|AnyCPU'">
      <OutputPath>bin\</OutputPath>
      <DefineConstants>DEBUG;TRACE</DefineConstants>
    </PropertyGroup>
    

    我将 .csproj 中的 Web.Test.config 更改为:

    <None Include="Web.Test.config">
      <DependentUpon>Web.config</DependentUpon>
    </None>
    

    收件人:

    <Content Include="Web.Test.config">
      <DependentUpon>Web.config</DependentUpon>
    </Content>
    

    【讨论】:

    • 更改为 对我有用,尽管它确实将文件复制到部署的站点,这不是很好
    猜你喜欢
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2015-03-11
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多