web.config変換を使用した高度なタスク
-
04-10-2019 - |
質問
値全体や属性を置き換える代わりに、値の特定のセクションを「変換」する方法があるかどうかを知っていますか?
たとえば、さまざまなWebサービスのURLを指定するいくつかのAppSettingsエントリがあります。これらのエントリは、開発環境とは開発環境でわずかに異なります。他の人よりも些細なものもありません
<!-- 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」from ".prod"。 2番目のエントリでは、サブドメインが異なります。 「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" />
ただし、そのWebServiceのバージョンが更新されるたびに、web.release.configも更新する必要があります。
また、そのURLを異なるセクションに分割して独立して更新することもできますが、1つのキーにすべてを入れています。
私は利用可能なweb.config Transformsを調べましたが、私が達成しようとしていることを調整していないようです。
これらは、私が参照として使用しているウェブサイトです。
Vishal Joshiのブログ, MSDNヘルプ, 、 と Channel9ビデオ
どんな助けも大歓迎です!
-d
解決
実際のところ、あなたはこれを行うことができますが、それはあなたが思うほど簡単ではありません。独自の構成変換を作成できます。私は非常に詳細なブログ投稿を書いたばかりです http://sedodream.com/2010/09/09/extendingxmlwebconfigconfigtransformation.aspx これについて。しかし、ここにハイライトがあります:
- クラスライブラリプロジェクトを作成します
- 参照web.publishing.tasks.dll (%プログラムファイル(x86)%msbuild microsoft visualstudio v10.0 webフォルダー)
- Microsoft.web.publishing.tasks.transform classを拡張します
- 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を参照する必要があります。