Web Deploy / Publish正在添加一个未知的连接字符串?

21

之前发布我的 Web API 应用程序时一切运作良好。今天我将应用程序从“任何 CPU”转换为 x86。现在当我发布它时,它会向 web.config 添加一个连接字符串,这导致我的应用程序失败。起初我以为可能是我不小心向 web.config 转换文件中添加了一些东西,但没有。以下是我的转换文件:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <entityFramework xdt:Transform="Replace">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Server=MyWebServer;Database=RunLog;Trusted_Connection=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

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

这是我的Web配置文件:

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

最终,我发布后,我的web.config文件看起来是这样的:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Server=MyWebServer;Database=RunLog;Trusted_Connection=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="RunLog" connectionString="RunLog_ConnectionString" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

如果你看上面,你会看到一个连接字符串被添加了。

经过一些研究,我发现您可以在发布期间添加连接字符串。 我一直使用同一个配置文件进行发布,因此我多次删除了配置文件并创建了一个新的,并确保取消选中添加连接字符串的框:

Publishing
<?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 http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <MSDeployServiceURL>http://MyWebServer</MSDeployServiceURL>
    <DeployIisAppPath>My Web Site/Go</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>RemoteAgent</MSDeployPublishMethod>
    <UserName>JohnBob</UserName>
    <_SavePWD>False</_SavePWD>
    <PublishDatabaseSettings>
      <Objects xmlns="">
        <ObjectGroup Name="RunLog" Order="1" Enabled="False">
          <Destination Path="" />
          <Object Type="DbCodeFirst">
            <Source Path="DBContext" DbContext="RunLog.DL.RunLogContext, RunLog.DL" Origin="Convention" />
          </Object>
        </ObjectGroup>
      </Objects>
    </PublishDatabaseSettings>
  </PropertyGroup>
  <ItemGroup>
    <MSDeployParameterValue Include="$(DeployParameterPrefix)RunLog-Web.config Connection String">
      <UpdateDestWebConfig>False</UpdateDestWebConfig>
    </MSDeployParameterValue>
  </ItemGroup>
  <ItemGroup>
    <_ConnectionStringsToInsert Include="RunLog" />
  </ItemGroup>
</Project>

您可以看到其中有连接字符串的内容,但我明确取消了“在运行时使用此连接字符串(更新目标Web配置文件)”。如果我删除添加的连接字符串,我的网站就可以完美工作。

有人知道出了什么问题吗?

编辑:
我想也许我做错了什么,但今晚经过努力工作后,我认为这可能是一个错误。我决定回滚所有代码并查看是否可以重现这个问题。以前在“包/发布 Web”选项卡中,在属性中选择“包/发布 SQL 选项卡中配置的所有数据库”。在“包/发布 SQL”选项卡中,“数据库条目”中定义了一个连接字符串,但其为空白。如果我将其保持原样并尝试发布,则一切正常。如果我删除连接字符串并重新添加它,则会再次出现问题。在上面的对话框中,如果数据库部分为空,则在创建配置文件时一切都正常工作,但如果有远程连接字符串框,则不会正确工作。


4
我有同样的问题,发布向导会自动将连接字符串添加到已发布的web.config文件中,而我想避免这种情况发生。 - Etienne
你能否添加你的Web.Release.config文件内容,因为那里可能有一个转换正在添加连接字符串。 - MarkG
1
你好,user127954,你解决了这个问题吗?我也遇到了同样的问题。即使我明确告诉Web Deploy不要添加虚拟连接字符串,但它仍会为上下文添加一个虚拟连接字符串。我正在使用一个默认的ConnectionFactory,它根据用户登录的子域创建连接字符串。但由于虚拟连接字符串的创建,这个默认的ConnectionFactory变得无用了。 - user3042320
嗨@user3042320,我有完全相同的问题。你找到解决方案了吗? - To Ka
@ToKa 很遗憾,不行。那只是我在家里的一个小项目,但很不幸它已经不存在了。但是几年后,我们最近开发了一个新的MVC 5应用程序,并遇到了完全相同的问题。这导致我们的网站崩溃,因为我们正在加密连接字符串。我们不得不进行一些hackery来修复它,但它肯定仍然是一个问题。 - coding4fun
难以置信,这个问题居然完全没有得到解决,而且现在已经是2019年10月27日了。Visual Studio 2019仍然存在这个问题。点击编辑设置,取消所有“在运行时使用此连接字符串”选项的勾选,保存后,它仍会添加它们,当你再次进入选项时,它们又都被勾选上了。这种行为非常恶心,它会覆盖Web.config中的任何连接设置,并强制注入自己的连接字符串,只因为你使用了设置页面。所以基本上,你永远无法编辑设置或使用设置编辑器,因为它是坏掉的。 - Triynko
4个回答

8

通过编辑XML,将此项目属性添加到.csproj文件中:

<Project>
  <PropertyGroup>

    <AutoParameterizationWebConfigConnectionStrings>false</AutoParameterizationWebConfigConnectionStrings>

    ...
  </PropertyGroup>
  ...
</Project>

或者,按照相关的StackOverflow问题中所述,使用Web发布管道目标文件。


10
这对我似乎没有用。即使将这个属性放进去,每次它仍然会将连接字符串放入部署的web.config文件中。 - Brannon
你能再详细解释一下这个解决方案吗? - Ross Brasseaux

3

编辑项目的.csproj文件,添加以下内容:

<PropertyGroup>
<InsertAdditionalWebCofigConnectionStrings>false</InsertAdditionalWebCofigConnectionStrings>

对我来说有效。


1

0
在 .pubxml 文件中,由于某种原因添加了一些额外的连接字符串。删除它们,应该就可以正常工作了,但默认的连接字符串仍将被添加。
我有一个预感,这是在我调整发布信息时添加到 .pubxml 中的,但当我切换回常规发布设置时,它没有被删除。
<ItemGroup>
    <MSDeployParameterValue Include="$(DeployParameterPrefix)DefaultConnection-Web.config Connection String" />
    <MSDeployParameterValue Include="$(DeployParameterPrefix)MyProject-Web.config Connection String" />
    <MSDeployParameterValue Include="$(DeployParameterPrefix)MyProject_dev-Web.config Connection String" />
  </ItemGroup>
  <ItemGroup>
    <_ConnectionStringsToInsert Include="MyProject" />
    <_ConnectionStringsToInsert Include="Myproject_dev" />
  </ItemGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接