使用web.config转换的高级任务
-
04-10-2019 - |
题
有人知道是否有一种方法可以“转换”值的特定部分,而不是替换整个值或属性?
例如,我有几个AppSettings条目为不同的Web服务指定URL。这些条目在开发环境中与生产环境略有不同。有些比其他人更不重要
<!-- 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>
请注意,在拳头条目中,唯一的区别是 “ .dev”来自“ .prod”。 在第二个条目中,子域是不同的: “ ma1-lab.lab1” 从 “ WS.ServiceName2”
到目前为止,我知道我可以在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,这会破坏简化我的web.config更新的目的。
我知道我也可以将该URL分为不同的部分并独立更新,但我宁愿将其全部放在一个密钥中。
我已经浏览了可用的web.config变换,但似乎没有我想完成的工作。
这些是我用作参考的网站:
Vishal Joshi的博客, MSDN帮助, , 和 Channel9视频
任何帮助将非常感激!
-d
解决方案
实际上,您可以做到这一点,但这并不像您想象的那么容易。您可以创建自己的配置转换。我刚刚写了一篇非常详细的博客文章 http://sedodream.com/2010/09/09/extendingxmlwebconfigconfigtransformation.aspx 关于这个。但这是Hightlights:
- 创建班级库项目
- 参考web.publishing.tasks.dll (在%program文件(x86)%MSBUILD MICROSOFT VISUALSTUDIO V10.0 WEB文件夹)
- 扩展Microsoft.web.publishing.tasks.transform类
- 实施apply()方法
- 将集会放在一个众所周知的位置
- 使用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>
这是我的配置转换文件
<?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>
其他提示
作为更新,如果您使用的是Visual Studio 2013,则应参考%程序文件(X86)%MSBUILD MICROSOFT VISUALSTUDIO V12.0 WEB MICROSOFT.WEB.XMLTRANSFORM.DLL。