MSBuild Web Deploy不更新连接字符串

时间:2013-02-13 13:33:36

标签: msbuild web-deployment publishing msdeploy webdeploy

我目前正在尝试在生产服务器上启动并运行部署过程。目前我正在使用Web部署和发布配置文件来实现这一点,除了更新连接字符串以适应生产服务器之外,我还能正常工作。

我正在使用:

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文件中获取正确的字符串?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分: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="" />

只需添加您需要的任意数量,并显然根据需要填充属性

答案 1 :(得分:0)

我的DefaultConnection也没有更新。事实证明我必须删除MyProject&gt; PublishProfiles .pubxml文件。

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

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

所以在azure控制面板(门户网站)中,我点击了网站&gt;我的网站&gt;配置

向下滚动到连接字符串,您可以显示隐藏的连接字符串。我只是通过点击x然后在我的网络配置中硬编码了正确的一个来删除它。

然后我再次移除.pubxml并再次通过向导。现在没有使用发布配置文件下拉连接字符串。

答案 2 :(得分:0)

我的发布配置文件.pubxml文件(位于Project \ Properties \ PublishProfiles中)已损坏,并带有多余的“ DefaultConnection-Web.config连接字符串”节点。删除多余的节点后,连接字符串正确更新。