5

web.configの複数のWCFサービスでIPアドレスを切り替える必要があります。web.config変換では、xpathですべてのアドレスを指定する以外に、検索と置換のステートメントを作成する方法はありますか。たとえば、1.2.3.4のすべてのインスタンスに対して、IPアドレス1.2.3.4を4.3.2.1に切り替えます。

4

1 に答える 1

4

Web.configが次のようなものであるとします(単純化されたシナリオですが、// XPathではどこでも機能します):

<configuration>
    <endpoint address="1.2.3.4" />
    <endpoint address="1.2.3.4" />
    <endpoint address="1.2.3.4" />
    <endpoint address="1.2.3.4" />
</configuration>

次に、次のようなものが必要になります。

<?xml version="1.0"?>    
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->    
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

    <replaceAll>
        <endpontAddresses xdt:Locator="XPath(//endpoint[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />
    </replaceAll>

</configuration>

注:このXPathはすべてを検索しますWeb.config全体の要素であり、指定された要素に「1.2.3.4」に等しい値のアドレス属性があるかどうかを確認します。より一般的なものが必要な場合は、これを試してください。

<?xml version="1.0"?>
    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

        <replaceAll>
            <endpontAddresses xdt:Locator="XPath(//*[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />
        </replaceAll>

    </configuration>

これにより、すべてのXML要素(アスタリスク:*による)が調べられ、値が「1.2.3.4」に等しいアドレス属性があるかどうかがチェックされます。したがって、これは次のようなファイルで機能します。

<configuration>
    <endpoint name="serviceA" address="1.2.3.4" />
    <endpoint name="serviceB" address="1.2.3.4" />
    <endpoint name="serviceC" address="1.2.3.4" />
    <endpoint2 address="1.2.3.4" />
    <endpoint3 address="1.2.3.4" />
    <endpoint4 address="1.2.3.4" />

    <innerSection>
    <endpoint address="1.2.3.4" />
    <anotherEndpoint address="1.2.3.4" />
    <sampleXmlElement address="1.2.3.4" />
    </innerSection>
</configuration>

ここで、置換を特定のセクションに制限する場合、つまり<system.serviceModel>、次のようなXPathを使用できます。

    <endpontAddresses xdt:Locator="XPath(/configuration/system.serviceModel//*[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />

<system.serviceModel>これにより、セクション内のアドレスのみが更新されます

<configuration>
    <endpoint name="serviceA" address="1.2.3.4" />
    <endpoint name="serviceB" address="1.2.3.4" />
    <endpoint name="serviceC" address="1.2.3.4" />
    <endpoint2 address="1.2.3.4" />
    <endpoint3 address="1.2.3.4" />
    <endpoint4 address="1.2.3.4" />

    <innerSection>
    <endpoint address="1.2.3.4" />
    <anotherEndpoint address="1.2.3.4" />
    <sampleXmlElement address="1.2.3.4" />
    </innerSection>

    <system.serviceModel>
    <endpoint name="serviceB" address="1.2.3.4" />
    <endpoint name="serviceC" address="1.2.3.4" />
    <endpoint2 address="1.2.3.4" />
    <innerSection>
      <endpoint address="1.2.3.4" />
      <anotherEndpoint address="1.2.3.4" />
      <sampleXmlElement address="1.2.3.4" />
      </innerSection>
    </system.serviceModel>

</configuration>

それらを試してみて、あなたのニーズに最も適したものを選択してください。

注:これには、IP(1.2.3.4)を含む属性の名前を指定する必要があるという制限がありますが、ここで魔法を発生させるよりも明示的にする方がよいと思います。多くの属性名がある場合は、

于 2012-04-02T16:09:53.937 に答える