c# – 根据环境更改WCF服务引用URL
我有一个使用许多WCF服务的Web应用程序.我在各种环境(开发,UAT,生产等)中部署我的Web应用程序.每个WCF服务的URL对于每个环境都是不同的.我使用的是.NET 3.5 andbasicHttpBindings
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吗? 解决方法
一种可能的方法是“外化”< system.serviceModel>的某些部分.配置到外部文件,每个环境一个.
例如.我们有“bindings.dev.config”和“bindings.test.config”,然后我们在主web.config中引用,如下所示: <system.serviceModel> <bindings configSource="bindings.dev.config" /> </system.serviceModel> 这样,你需要从DEV更改为PROD就是这一行的配置XML. 基本上,在.NET 2.0配置中,任何配置元素都可以“外部化”.但是,您无法直接外部化configGroups(例如“system.serviceModel”) – 您必须处于“配置元素”级别. 渣 编辑:好的,所以NO配置编辑更改以在环境之间切换….. 就像是: <bindings> <binding name="Default_DEV"> ..... </binding> <binding name="Default_PROD"> ..... </binding> </bindings> 这样,您可以从您的代码和您正在运行的环境中构建所需元素的名称(例如绑定“Default_PROD”),然后从配置文件中获取相应的配置,其中包含所有配置设置所有环境. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |