我有一个使用大量 WCF 服务的 Web 应用程序。我在各种环境(开发、UAT、生产等)中部署我的 Web 应用程序。每个 WCF 服务的 URL 因环境而异。我正在使用 .NET 3.5 和 basicHttpBinding
Web 应用程序使用一个框架来支持我的 web.config 文件中特定于机器的设置。在实例化 WCF 服务客户端的实例时,我调用了一个函数,该函数使用采用参数的构造函数重载创建 WCF 服务客户端的实例:
System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress
本质上是<system.serviceModel><bindings><basicHttpBinding><binding>
web.config 中的配置已复制到 C# 代码中。
这种方法效果很好。
但是,我现在必须增强此方法以使用使用 X509 证书的 WCF 服务。这意味着我必须在 C# 代码的 web.config 中复制以下附加设置:
<!-- inside the binding section -->
<security mode="Message">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="Certificate" algorithmSuite="Default" />
</security>
<behaviors>
<endpointBehaviors>
<behavior name="MyServiceBehaviour">
<clientCredentials>
<clientCertificate storeLocation="LocalMachine" storeName="My"
x509FindType="FindByThumbprint" findValue="1234abcd" />
<serviceCertificate>
<defaultCertificate storeLocation="LocalMachine" storeName="My"
x509FindType="FindByThumbprint" findValue="5678efgh" />
<authentication trustedStoreLocation="LocalMachine"
certificateValidationMode="None" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
我在弄清楚如何用 C# 编写此配置时遇到了一些困难。
两个问题
- 谁能推荐一种更好的方法来跨多个环境管理 WCF 服务引用 URL?
- 或者,欢迎就如何在 C# 中复制上述 web.config 部分提出任何建议
最佳答案
一种可能的方法是将
例如我们有“bindings.dev.config”和“bindings.test.config”,然后我们在主 web.config 中引用它们,如下所示:
<system.serviceModel>
<bindings configSource="bindings.dev.config" />
</system.serviceModel>
这样,从 DEV 更改为 PROD 所需要做的就是这一行配置 XML。
基本上,在 .NET 2.0 配置中,任何配置元素都可以“外部化”。但是,您不能直接外部化配置组(例如“system.serviceModel”)——您必须处于“配置元素”级别。
马克
编辑:好的,所以NO 配置编辑更改以在环境之间切换...... 在那种情况下,您可能必须想出一个命名方案,例如以您可以在运行时区分它们的方式命名您的绑定(bind)、行为和端点。
类似于:
<bindings>
<binding name="Default_DEV">
.....
</binding>
<binding name="Default_PROD">
.....
</binding>
</bindings>
那样的话,您可以从您的代码和您正在运行的环境中构建您想要的元素的名称(例如绑定(bind)“Default_PROD”),然后从包含所有元素的配置文件中获取相应的配置所有环境的配置设置。
关于c# - 根据环境更改 WCF 服务引用 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/797539/