Dot-Net
WCF:單個服務的多個綁定配置
我正在開發一個必須支持向後兼容性的客戶端-伺服器應用程序(.NET 4 WPF、WCF)。換句話說,就操作合約和數據合約而言,舊客戶端應該與新伺服器兼容(反之亦然)。
我們的 WCF 服務託管在 IIS 中,它們被設置為使用 basicHttpBinding:
<basicHttpBinding> <binding name="basicHttpBinding_Configuration" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" /> <security mode="None" /> </binding> </basicHttpBinding> ... <service behaviorConfiguration="SampleGateway.Data.DataAccessBehavior" name="SampleGateway.Data.DataAccess"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_Configuration" contract="Sample.Data.IDataAccess" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8731/Design_Time_Addresses/SampleGateway/SampleGateway.Data.DataAccess.svc" /> </baseAddresses> </host> </service> ... <behavior name="SampleGateway.Data.DataAccessBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <dataContractSerializer maxItemsInObjectGraph="2147483647" /> </behavior>假設契約非常基本,看起來像這樣:
[ServiceContract] public interface IDataAccess { [OperationContract] List<Data> GetData(List<int> ids, DateTime startDateTime, DateTime endDateTime); }最近,我發現我們可以將編碼從 更改
XML為binary. 結合 IIS 壓縮,這確實提高了我們的 WCF 方法(例如上面列出的 GetData)的性能。此編碼更改還需要更改客戶端和伺服器 WCF 綁定,從 a 切換
basicHttpBinding到customBinding.<customBinding > <binding name="binaryHttpBinding_Configuration"> <binaryMessageEncoding maxReadPoolSize="2147483647" maxSessionSize="2147483647" maxWritePoolSize="2147483647"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647"/> </binaryMessageEncoding> <httpTransport transferMode="Streamed" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true"/> </binding> </customBinding> ... <service behaviorConfiguration="SampleGateway.Data.DataAccessBehavior" name="SampleGateway.Data.DataAccess"> <endpoint address="" binding="customBinding" bindingConfiguration="binaryHttpBinding_Configuration" contract="CEMLink.Data.IDataAccess" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8731/Design_Time_Addresses/SampleGateway/SampleGateway.Data.DataAccess.svc" /> </baseAddresses> </host> </service> ...這就是問題所在。由於我們的軟體必須支持客戶端/伺服器向後兼容,如果舊客戶端
basicHttpBinding嘗試使用新客戶端訪問伺服器customBinding,則呼叫將因不匹配而失敗,例如"Content Type text/xml; charset=utf-8 was not supported by this service.... The client and service bindings may be mismatched"我可以為同一個服務契約有兩種綁定配置——一種是基本的,另一種是自定義的,它們都指向同一個介面?我該如何解決這個問題?
對於在不同地址公開並與不同綁定對齊的相同服務,您基本上需要 2 個端點。 這可能會對您有所幫助。
對於同一個服務契約,您可以有 2 個不同的綁定,但是您需要在配置中創建單獨的服務節點,並且您還需要定義單獨的端點。因此,為二進制格式的服務創建一個新端點,並讓新版本的客戶端引用它。