【问题标题】:change connectionstrings with msbuild使用 msbuild 更改连接字符串
【发布时间】:2010-07-06 21:28:07
【问题描述】:

我正在尝试在 Teamcity 服务器上使用 MSBUILD 更改我的 web.config 中的 connectionString。以前我在一个目标中使用了这个属性:

<PropertyGroup>
<UpdateWebConfigCode>
 <![CDATA[
  public static void ScriptMain()
  {
   XmlDocument wcXml = new XmlDocument();
   wcXml.Load(@"TCM.MVC.UI\Web.config");

   XmlElement root = wcXml.DocumentElement;
   XmlNodeList connList = root.SelectNodes("//connectionStrings/add");
   XmlElement elem;

   foreach (XmlNode node in connList)
   {
    elem = (XmlElement)node;

    switch (elem.GetAttribute("name"))
    {
     case "TCMBaseConnectionString":
      elem.SetAttribute("connectionString", "Data Source=server-name;Initial Catalog=TCMCentral;User ID=user;Password=something");
      break;

    }
   }

   wcXml.Save(@"TCM.MVC.UI\Web.config");          

  }
 ]]>
</UpdateWebConfigCode>

然后我会在目标中调用它:

<Target Name="UpdateWebConfig">         
   <Script Language="C#" Code="$(UpdateWebConfigCode)" Imports="System.Xml" /> 
</Target>

但这一直抛出错误。我意识到这可能有点过时了,但找不到任何替代它的东西....有什么建议吗?

【问题讨论】:

    标签: c# msbuild


    【解决方案1】:

    我最终使用了 MSBuildCommunityTasks XmlUpdate 属性。以下是我的目标:

    <Target Name="UpdateWebConfig">        
        <XmlUpdate XmlFileName="C:\TCM.NET\Current\TCM.MVC.UI\web.config" XPath="configuration/connectionStrings/add[@name='TCMBaseConnectionString']/@connectionString" Value="Data Source=server-name;Initial Catalog=TCMCentral;User ID=user;Password=something" />
    </Target>
    

    这对我很有用。

    【讨论】:

    • 不错的选择,我也开始改用更通用的“UpdateXml”类型任务,尽管是我自己的创作而不是 MSBuildCommunity。
    【解决方案2】:

    我使用自定义任务执行此操作,该任务与您的代码大致相同,但 MSBuidl 变为:

    <UpdateConnectionString ConfigFile ="path\to\web.config"
             ConnectionStringName="MyConnectionStringName"
             ConnectionString="connection-string-here"/>
    

    这样一个任务的代码是

    public class UpdateConnectionString : Task
        {
            [Required]
            public string ConfigFile { get; set; }
            [Required]
            public string ConnectionStringName { get; set; }
            [Required]
            public string ConnectionString { get; set; }
    
            public override bool Execute()
            {
                try
                {
                    var fi = new FileInfo(ConfigFile);
                    if(!fi.Exists)
                    {
                        Log.LogError("File {0} does not exist");
                        return false;
                    }
                    fi.IsReadOnly = false;
                    XDocument doc = XDocument.Load(ConfigFile);
                    var confignode = doc.Descendants("configuration").Single();
                    var connectionStrings = confignode.Descendants("connectionStrings").SingleOrDefault();
                    if(connectionStrings == null)
                    {
                        connectionStrings = new XElement("connectionStrings");
                        confignode.Add(connectionStrings);
                    }
                    var connectionElement = connectionStrings.Descendants("add").SingleOrDefault(
                        e => e.Attribute("name") != null &&
                             string.Compare(e.Attribute("name").Value, ConnectionStringName,
                                            StringComparison.OrdinalIgnoreCase) == 0);
                    if (connectionElement == null)
                    {
                        connectionElement = new XElement("add", new XAttribute("name", ConnectionStringName));
                        connectionStrings.Add(connectionElement);
                    }
    
                    connectionElement.SetAttributeValue("connectionString", ConnectionString);
                    doc.Save(ConfigFile);
                }
                catch (Exception ex)
                {
                    Log.LogErrorFromException(ex, true);
                    return false;
                }   
                return true;
            }
        }
    

    请注意,如果它不存在,此代码还将添加一个连接字符串,这可能比您现在需要的更复杂。

    【讨论】:

    • 杰米,感谢您的帮助。我最终在 MSBuildCommunityTasks 中使用了 XmlUpdate 属性。请看下面我的回答。再次感谢。安德鲁
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2018-09-20
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多