使用的Web.Config改造高级任务 [英] Advanced tasks using Web.Config transformation

查看:149
本文介绍了使用的Web.Config改造高级任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道,如果有就是要改造,而不是替换整个值或属性值的特定部分的方式?

Does anyone know if there is a way to "transform" specific sections of values instead of replacing the whole value or an attribute?

例如,我有指定不同web服务的URL数的appSettings项。这些条目都在开发环境比生产环境略有不同。有些人比别人少琐碎

For example, I've got several appSettings entries that specify the Urls for different webservices. These entries are slightly different in the dev environment than the production environment. Some are less trivial than others

<!-- DEV ENTRY -->
<appSettings>
 <add key="serviceName1_WebsService_Url" value="http://wsServiceName1.dev.domain.com/v1.2.3.4/entryPoint.asmx" />
 <add key="serviceName2_WebsService_Url" value="http://ma1-lab.lab1.domain.com/v1.2.3.4/entryPoint.asmx" />
</appSettings>

<!-- PROD ENTRY -->
<appSettings>
 <add key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.domain.com/v1.2.3.4/entryPoint.asmx" />
 <add key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.domain.com/v1.2.3.4/entryPoint.asmx" />
</appSettings>

请注意,在进入拳头,唯一的区别就是开发。从.prod在第二个条目,子域不同的是:MA1-lab.lab1 ws.ServiceName2

Notice that on the fist entry, the only difference is ".dev" from ".prod". On the second entry, the subdomain is different: "ma1-lab.lab1" from "ws.ServiceName2"

到目前为止,我知道我可以做的Web.Release.Config是这样的:

So far, I know I can do something like this in the Web.Release.Config:

<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.domain.com/v1.2.3.4/entryPoint.asmx" />
<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.domain.com/v1.2.3.4/entryPoint.asmx" />

不过,每次的版本为web服务的更新,我将不得不更新Web.Release.Config为好,这违背了simplfying我的web.config更新的目的。

However, everytime the Version for that webservice is updated, I would have to update the Web.Release.Config as well, which defeats the purpose of simplfying my web.config updates.

我知道我也可以拆分网址成不同的部分并独立地更新它们,但我宁愿这一切在一个密钥。

I know I could also split that URL into different sections and update them independently, but I rather have it all in one key.

我已经通过现有的web.config变换看了,但似乎没有任何事情是什么towars我试图完成至为目标。

I've looked through the available web.config Transforms but nothings seems to be geared towars what I am trying to accomplish.

这是我使用作为参考网站:

These are the websites I am using as a reference:

<一个href=\"http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html\">Vishal乔希的博客, MSDN帮助和的 Channel9的影片

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

-D

推荐答案

由于事实上,你可以做到这一点,但它并不像你想象的那么简单。你可以创建自己的配置转型。我刚才写了非常详细的博客文章在<一个href=\"http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx\">http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx对此。但这里是hightlights:

As a matter of fact you can do this, but it is not as easy as you might think. You can create your own config transformation. I have just written a very detailed blog post at http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx regarding this. But here are the hightlights:


  • 创建类库项目

  • 参考Web.Publishing.Tasks.dll的(下%程序文件(x86)%的MSBuild \\微软\\ VisualStudio的\\ 10.0 \\ Web文件夹)

  • 扩展Microsoft.Web.Publishing.Tasks.Transform类

  • 实施应用()方法

  • 将装配在一个众所周知的位置

  • 使用XDT:导入使新改造现有

  • 使用变换

这里是我创造这样做的类替换

namespace CustomTransformType
{
    using System;
    using System.Text.RegularExpressions;
    using System.Xml;
    using Microsoft.Web.Publishing.Tasks;

    public class AttributeRegexReplace : Transform
    {
        private string pattern;
        private string replacement;
        private string attributeName;

        protected string AttributeName
        {
            get
            {
                if (this.attributeName == null)
                {
                    this.attributeName = this.GetArgumentValue("Attribute");
                }
                return this.attributeName;
            }
        }
        protected string Pattern
        {
            get
            {
                if (this.pattern == null)
                {
                    this.pattern = this.GetArgumentValue("Pattern");
                }

                return pattern;
            }
        }

        protected string Replacement
        {
            get
            {
                if (this.replacement == null)
                {
                    this.replacement = this.GetArgumentValue("Replacement");
                }

                return replacement;
            }
        }

        protected string GetArgumentValue(string name)
        {
            // this extracts a value from the arguments provided
            if (string.IsNullOrWhiteSpace(name)) 
            { throw new ArgumentNullException("name"); }

            string result = null;
            if (this.Arguments != null && this.Arguments.Count > 0)
            {
                foreach (string arg in this.Arguments)
                {
                    if (!string.IsNullOrWhiteSpace(arg))
                    {
                        string trimmedArg = arg.Trim();
                        if (trimmedArg.ToUpperInvariant().StartsWith(name.ToUpperInvariant()))
                        {
                            int start = arg.IndexOf('\'');
                            int last = arg.LastIndexOf('\'');
                            if (start <= 0 || last <= 0 || last <= 0)
                            {
                                throw new ArgumentException("Expected two ['] characters");
                            }

                            string value = trimmedArg.Substring(start, last - start);
                            if (value != null)
                            {
                                // remove any leading or trailing '
                                value = value.Trim().TrimStart('\'').TrimStart('\'');
                            }
                            result = value;
                        }
                    }
                }
            }
            return result;
        }

        protected override void Apply()
        {
            foreach (XmlAttribute att in this.TargetNode.Attributes)
            {
                if (string.Compare(att.Name, this.AttributeName, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    // get current value, perform the Regex
                    att.Value = Regex.Replace(att.Value, this.Pattern, this.Replacement);
                }
            }
        }
    }
}

这里是web.config中

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="one" value="one"/>
    <add key="two" value="partial-replace-here-end"/>
    <add key="three" value="three here"/>
  </appSettings>
</configuration>

下面是我的配置转换文件

Here is my config transform file

<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <xdt:Import path="C:\Program Files (x86)\MSBuild\Custom\CustomTransformType.dll"
              namespace="CustomTransformType" />

  <appSettings>
    <add key="one" value="one-replaced" 
         xdt:Transform="Replace" 
         xdt:Locator="Match(key)" />
    <add key="two" value="two-replaced" 
         xdt:Transform="AttributeRegexReplace(Attribute='value', Pattern='here',Replacement='REPLACED')" 
         xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

这是改造后的结果

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="one" value="one-replaced"/>
    <add key="two" value="partial-replace-REPLACED-end"/>
    <add key="three" value="three here"/>
  </appSettings>
</configuration>

这篇关于使用的Web.Config改造高级任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆