我现在只想更新 WCF 端点 URL 的一部分。现在我们通过为每个“品种”的所有端点包含不同的配置来做到这一点。这管理起来很繁琐。为此,我想在 web.config 中设置一个转换。
这是两个文件示例
开发
<endpoint address="http://servicesdev.host.com/RPUtilityServices/LogException.svc/restService"
behaviorConfiguration="restfulBehavior"
binding="webHttpBinding"
contract="Host.RP.Shared.Common.Services.Utility.Interfaces.IExceptionUtilityService"
name="LogService" />
还有一些
暂存
<endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
behaviorConfiguration="restfulBehavior"
binding="webHttpBinding"
contract="Host.RP.Shared.Common.Services.Utility.Interfaces.IExceptionUtilityService"
name="LogService" />
区别在于 servicessta 与 servicesdev。现在我还有 servicesuat 和 servicesqa 等。我想设置一个转换以仅将“dev”替换为“sta”等而不是整个 block (使用 xdt:Transform="Replace"
)
但是我该怎么做呢?
最佳答案
上面第一段代码(针对开发环境)可以到Web.config
(或 Web.debug.config
但还必须添加 xdt
转换)。在你的Web.release.config
(这个将进入暂存环境)定义以下元素。
<endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
behaviorConfiguration="restfulBehavior"
binding="webHttpBinding"
contract="Host.RP.Shared.Common.Services.Utility.Interfaces.IExceptionUtilityService"
name="LogService" xdt:Transform="Replace" />
请注意,我在发布配置文件中添加了 xdt:Transform="Replace"。使用此属性显示 endpoint
中定义的设置元素将替换您基地中的元素 Web.config
文件。
有关详细信息,请参阅 MSDN .
更新:
使用 xdt:Transform="Replace"
将替换整个 <endpoint />
元素。有选择地替换 address
<endpoint />
的属性元素使用以下转换。
<endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
xdt:Transform="SetAttributes(address)"/>
(请注意,如果有多个 <endpoint />
元素,您可能还想使用 Locator 属性。)
我所说的在我上面发布的 MSDN 页面上有详细描述。
关于c# - 使用 XDT 转换的 Web.config 进行部分替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18151158/