【问题标题】:How can I automatically verify that all web.config transforms have the same elements?如何自动验证所有 web.config 转换是否具有相同的元素?
【发布时间】:2017-04-30 11:00:48
【问题描述】:

我正在开发一个 ASP.NET 项目,该项目具有在构建时生成的多个 web.config 转换(使用 MsBuild 一次性);每个部署环境一个 .config 文件。

前:

  <Target Name="BeforeBuild">
      <TransformXml 
           Source="Web.Base.config"    
           Transform="Web.DevServer1.config"
           Destination="ConfigBuild\Web.DevServer1.config" />
      <TransformXml 
           Source="Web.Base.config"    
           Transform="Web.QAServer1.config"
           Destination="ConfigBuild\Web.QAServer1.config" />
      <!-- ... -->
      <!-- ... -->
  </Target>

每个转换都有几个元素,其值被替换到基本 web.config 文件中。管理层和我担心一个必要的元素可能会在其中一个转换文件中被错误地忽略。

有没有一种方法可以使用 MsBuild 或其他一些 VisualStudio 工具自动验证我们的转换文件是否缺少任何元素?

最好在构建时执行此检查。

【问题讨论】:

  • 离题了,但是 WCT 的问题在于,在构建系统之前,一些本质上应该是已知的和静态的东西(比如每个环境的配置)还没有完全定义。如果你想做出改变,你在技术上必须重建。这使得很难将两者分开,这当然应该是这种情况。
  • 您解决了这个问题吗,如果问题仍然存在,请随时告诉我。
  • 抱歉回复晚了。是的,这个答案很有帮助。

标签: c# asp.net .net visual-studio msbuild


【解决方案1】:

有没有一种方法可以使用 MsBuild 或其他 VisualStudio 工具自动验证我们的所有转换文件都没有丢失任何元素?

您可以创建一个自定义任务,使用 XmlDocument 类比较配置文件,如果它们不同,则使用 Log.LogMessage 输出节点消息。像这样:

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Xml;

namespace Common
{
    public class SimpleTask3 : Task
    {
        private string myProperty;

        // The [Required] attribute indicates a required property.
        // If a project file invokes this task without passing a value
        // to this property, the build will fail immediately.
        [Required]
        public string MyProperty
        {
            get
            {
                return myProperty;
            }
            set
            {
                myProperty = value;
            }
        }

        public override bool Execute()
        {
            // Log a high-importance comment
            Log.LogMessage(MessageImportance.High, "The task was passed \"" + myProperty + "\"");
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(myProperty + "/web.base.config");

            XmlDocument sDoc = new XmlDocument();
            sDoc.Load(myProperty + "/ConfigBuild/Web.DevServer1.config");

            //compare with them and check the different.

            //if different

            Log.LogMessage(MessageImportance.High, "different message");
            return true;
        }
    }
}

Web.config:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

  <Target Name="BeforeBuild">
    <!--<Message Text="BuildDependsOn: $(BuildDependsOn)" />-->
    <Message Text="Inside of BeforeBuild, time: $([System.DateTime]::Now)" />
    <TransformXml Source="D:\Project\Msbuild\App1\App2\Web.Base.config" Transform="D:\Project\Msbuild\App1\App2\Web.DevServer1.config" Destination="D:\Project\Msbuild\App1\App2\ConfigBuild\Web.DevServer1.config" />
    <TransformXml Source="D:\Project\Msbuild\App1\App2\Web.Base.config" Transform="D:\Project\Msbuild\App1\App2\Web.QAServer1.config" Destination="D:\Project\Msbuild\App1\App2\ConfigBuild\Web.QAServer1.config" />
  </Target>

  <UsingTask TaskName="Common.SimpleTask3"
        AssemblyFile="D:\Project\Msbuild\App1\Common\bin\Debug\Common.dll"/>
  <Target Name="AfterBuild">
    <SimpleTask3 MyProperty="D:\Project\Msbuild\App1\App2"/>
  </Target>

【讨论】:

    猜你喜欢
    • 2018-03-02
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多