我有一個使用多個WCF服務的Web應用程序。我在各種環境(dev,UAT,production等)中部署我的web應用程序。每個WCF服務的URL對於每個環境都是不同的。我正在使用.NET 3.5和basicHttpBinding
s根據環境更改WCF服務參考URL
Web應用程序使用框架來支持web.config文件中的計算機特定設置。當實例化一個WCF服務客戶端的一個實例,我認爲這種使用構造函數重載需要的參數創建WCF服務客戶端的實例函數:
System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress
本質web.config中的<system.serviceModel><bindings><basicHttpBinding><binding>
配置已被複制在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服務參考網址更好的方法?
- 另外,關於如何複製C#中的上述web.config部分,任何建議將受到歡迎
爲我的答案增加了一些額外的想法 - 您可以使用命名綁定等方式,以便您可以根據您的環境確定名稱,然後從配置文件中加載適當的值。 – 2009-04-28 15:33:28
下面的問題涉及到這個http://stackoverflow.com/questions/798684/programmatically-set-identity-on-wcf-endpoint地址 – 2009-04-30 09:20:32
我意識到你在代碼而不是在配置文件中這樣做,但爲什麼不使用配置轉換並將環境特定設置放置在適當的配置文件(Web.config.dev,Web.config.Test,Web.config.Release等)中? – camainc 2010-10-28 16:37:05