【问题标题】:Web.config with XDT transform to do partial replace带有 XDT 转换的 Web.config 进行部分替换
【发布时间】:2013-08-11 15:35:14
【问题描述】:

我的情况是,我只想更新 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' 等,而不是整个块(使用 xdt:Transform="Replace"

但是我该怎么做呢?

【问题讨论】:

  • 只是好奇通过替换 url 的子字符串而不是整个属性可以获得什么?
  • 我还有几个类似的块,restService,soap one 等。我将获得的是我只需要更新一个文件来替换服务地址而不是所有其他文件。我们有 5 个环境,因此替换使管理更容易。
  • 我想我误解了你的帖子。当您真的只想更改属性时,我认为您想在属性上运行某种子字符串(至少这是我从您标记为正确的答案中得到的)。
  • 不,你说得对,我只想替换子字符串。这是不可能的,所以我接受了正确的答案,因为它是下一个最佳选择。

标签: c# visual-studio-2012 web-config .net-4.5 web.config-transform


【解决方案1】:

上面的第一段代码(用于开发环境)可以转到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" 将替换整个&lt;endpoint /&gt; 元素。要选择性地替换 &lt;endpoint /&gt; 元素的 address 属性,请使用以下转换。

<endpoint address="http://servicessta.host.com/RPUtilityServices/LogException.svc/restService"
 xdt:Transform="SetAttributes(address)"/>

(请注意,如果有多个 &lt;endpoint /&gt; 元素,您可能还需要使用 Locator 属性。)

我上面贴的 MSDN 页面上详细描述了我所说的。

【讨论】:

  • 我熟悉如何在节点上进行替换(使用 xdt:Transform="Replace")。我只想替换字符串的一部分,所以我没有到处都重新定义。
  • 您能否展示一个具有多个端点和Locator 属性的示例?牛,在这里找到答案:stackoverflow.com/questions/4637107/…
  • 这根本没有回答问题。这不是子字符串替换。
  • 这并没有回答只替换部分字符串而不是整个字符串的问题。
  • 这没有回答问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2011-08-09
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多