MSBuild Web Deploy不能更新连接字符串

5

我目前正在尝试在我的生产服务器上建立部署流程。目前,我正在使用Web Deploy和发布配置文件来实现这一点,所有的东西都能正确地工作,除了更新连接字符串以适应生产服务器。

我正在使用:

msbuild myProj.csproj /p:DeployOnBuild=true;PublishProfile=myProfile;Configuration=Release

创建发布包,以及:
call myProj.deploy.cmd /Y /M:http://myServer/MSDeployAgentService -allowUntrusted /U:user /:Password

这样做是有效的,它可以将内容打包并成功地发送到服务器,并正确配置IIS,但指向了错误的数据库。

我的发布配置文件如下:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <MSDeployServiceURL>http://myserver</MSDeployServiceURL>
    <DeployIisAppPath>Website</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>RemoteAgent</MSDeployPublishMethod>
    <UserName>user</UserName>
    <_SavePWD>True</_SavePWD>
    <PublishDatabaseSettings>
      <Objects xmlns="">
        <ObjectGroup Name="DBContext" Order="1" Enabled="False">
          <Destination Path="Data Source=server;Initial Catalog=ProductionDB;User ID=user;Password=&quot;password&quot;" Name="" />
          <Object Type="DbCodeFirst">
            <Source Path="DBMigration" DbContext="myproj.Repositories.DBContext, myproj.Repositories" MigrationConfiguration="myproj.Repositories.Migrations.Configuration, myproj.Repositories" Origin="Configuration" />
          </Object>
        </ObjectGroup>
        <ObjectGroup Name="DefaultConnection" Order="2" Enabled="False">
          <Destination Path="Data Source=server;Initial Catalog=ProductionDB;User ID=user;Password=&quot;password&quot;" Name="" />
          <Object Type="DbDacFx">
            <PreSource Path="Data Source=localhost;Initial Catalog=devDB;User ID=user;Password=&quot;password&quot;" includeData="False" />
            <Source Path="$(IntermediateOutputPath)AutoScripts\DefaultConnection_IncrementalSchemaOnly.dacpac" dacpacAction="Deploy" />
          </Object>
          <UpdateFrom Type="Web.Config">
            <Source MatchValue="Data Source=localhost;Initial Catalog=devDB;User Id=user;Password=password" MatchAttributes="$(UpdateFromConnectionStringAttributes)" />
          </UpdateFrom>
        </ObjectGroup>
      </Objects>
    </PublishDatabaseSettings>
  </PropertyGroup>
  <ItemGroup>
    <MSDeployParameterValue Include="$(DeployParameterPrefix)DBContext-Web.config Connection String">
      <ParameterValue> Data Source=server;Initial Catalog=ProductionDB;User ID=user;Password="password"</ParameterValue>
    </MSDeployParameterValue>
    <MSDeployParameterValue Include="$(DeployParameterPrefix)DefaultConnection-Web.config Connection String">
      <ParameterValue>Data Source=server;Initial Catalog=ProductionDB;User ID=user;Password="password"</ParameterValue>
    </MSDeployParameterValue>
  </ItemGroup>
</Project>

很烦人的是,当直接从VS2012发布时,一切都正常,但通过命令行却不行。也许我在msbuild调用中漏了一个开关或选项?我的myProj.SetParameters.xml文件中显示的连接字符串不正确,因此如果我手动将它们更改为正确的连接字符串,则部署后生产服务器上的web.xml文件就是正确的。如何将正确的字符串放入我的SetParameters文件中?非常感谢您的帮助。

你确定你的第一个命令生成了一个包吗?你的发布配置文件的WebPublishMethod是“MSDeploy”,而不是“Package”。 - Richard Szalay
正在生成软件包。您可以使用MSDeploy进行部署,或者使用我正在使用的方法。 - Thewads
另外,您可以使用 web.config 转换来替换连接字符串,并在执行 MSBuild 时选择要发布的配置。 - ThomasArdal
@Thewads - 这对我来说似乎很奇怪。只有在CreatePackageOnPublishtrue或使用WebPublishMethodPackage的发布配置文件进行部署时,才应该生成包。您确定没有设置其他属性吗? - Richard Szalay
@RichardSzalay 我只是按照上面详细说明的命令运行。如果我去掉 DeployOnBuild=true,那么包就不会被构建。 - Thewads
3个回答

3
为了解决这个问题,在Visual Studio中,我创建了一个Parameters.xml文件放在项目的根目录中,该文件保存连接字符串的值,以便在生产服务器上使用。这些值将被读取并代替默认值使用。
Parameters.xml文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
    <parameter name="DefaultConnection-Web.config Connection String"
        description=""
        defaultValue=""tags="" />

只需添加所需数量的标签,并根据需要填充属性即可。


0

我的DefaultConnection也没有更新。结果发现我必须删除MyProject > PublishProfiles .pubxml文件。

然后尝试发布新构建的项目时,它要求我连接到Azure并下载发布配置文件。

即使该向导有一个复选框,可以选择关闭使用发布配置文件中拉下来的DefaultConnection字符串的覆盖选项,但取消选中它没有任何效果。它继续覆盖该字符串。

因此,在Azure控制面板(门户)中,我单击网站 > 我的网站 > 配置

向下滚动到连接字符串,您可以显示隐藏的连接字符串。我只需通过点击x将其删除,然后在我的Web.config中硬编码正确的连接字符串。

然后我再次删除了.pubxml文件,并再次运行了向导。现在没有连接字符串与发布配置文件一起拉下来。


0
我的发布配置文件.pubxml(位于Project\Properties\PublishProfiles中)因为有额外的重复“DefaultConnection-Web.config连接字符串”节点而损坏。在我删除了多余的节点后,连接字符串得到了正确的更新。

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