c# - 使用 XDT 转换的 Web.config 进行部分替换

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

我现在只想更新 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/

相关文章:

ASP.NET web.config : What is the default for the debug attribute in system. web.compilation?

c# - 添加到初始化的多维槽

c# - 如何在 Serilog Sink 中使用依赖注入(inject)?

c# - 在 EF 中插入/更新实体?

visual-studio-2012 - Visual Studio 2012 键盘快捷键/键绑定(bind)海报

entity-framework - 在 Visual Studio 2012 中使用 EF 脚手架创建新的 Web API Controller 时出现 "Object reference not set to an instance of an object"

c# - 没有麦克风? SpeechRecognitionEngine SetInputToDefaultAudioDevice() 问题

visual-studio-2008 - Visual Studio 2008 : No Intellisense in web. 配置

c# - 为特定路由增加 ServiceStack 中的 maxRequestLength

c# - 字典与数据库(Mysql)