我正在开发一个客户端-服务器应用程序 (.NET 4 WPF, WCF),它必须支持向后兼容。换句话说,旧的客户端应该与新的服务器 (反之亦然) 兼容操作契约和数据契约。
我们的 WCF 服务托管在 IIS 中,并且它们曾经设置为使用 basicHttpBinding:
我们的 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
更改为二进制
。结合IIS压缩,这确实提高了我们的WCF方法(例如上面列出的GetData)的性能。
这种编码更改还需要更改客户端和服务器WCF绑定,从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"
我可以为同一个服务契约拥有两个绑定配置,一个是基本的,另一个是自定义的,它们都指向同一个接口吗?如何解决这个问题?