WCF:单个服务的多个绑定配置 [英] WCF: Multiple binding configurations for a single service
问题描述
我正在处理必须支持向后兼容性的客户端服务器应用程序(.NET 4 WPF,WCF).换句话说,就操作合同和数据合同而言,旧客户端应与新服务器兼容(反之亦然).
I'm working on a client-server application (.NET 4 WPF, WCF) that must support backwards compatibility. In other words, old clients should be compatible with new servers (and vice versa) as far as operation contracts and data contracts.
我们的WCF服务托管在IIS中,并且已设置以使用basicHttpBinding:
Our WCF services are hosted in IIS, and they were set up to use 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>
假设合同很基本,看起来像这样:
Assume the contract is pretty basic and looks something like this:
[ServiceContract]
public interface IDataAccess
{
[OperationContract]
List<Data> GetData(List<int> ids, DateTime startDateTime, DateTime endDateTime);
}
最近,我发现我们可以将编码从 XML
更改为 binary
.结合IIS压缩,这确实提高了WCF方法(如上面列出的GetData)的性能.
Recently, I discovered that we could change our encoding from XML
to binary
. Combined with IIS compression, this really boosted the performance of our WCF methods such as GetData listed above.
此编码更改还需要更改客户端和服务器WCF绑定,从 basicHttpBinding
切换到 customBinding
.
This encoding change also required a change in the client and server WCF bindings, switching from a basicHttpBinding
to a 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
击中服务器,则调用将失败不匹配,例如内容类型为text/xml;此服务不支持charset = utf-8....客户端和服务的绑定可能不匹配"
Here's the problem. Since our software must support client/server backwards compatibility, if an old client with the old basicHttpBinding
tries to hit a server with the new customBinding
, the call is going to fail with a mismatch, e.g. "Content Type text/xml; charset=utf-8 was not supported by this service.... The client and service bindings may be mismatched"
我可以为同一服务合同使用两种绑定配置吗?一个是基本配置,另一个是自定义配置,它们都指向同一个接口?我该如何解决?
Can I have two binding configurations for the same service contract - one basic and the other custom, and they both point to the same interface? How can I work around this?
推荐答案
对于同一个服务,您基本上需要2个终结点,这些终结点公开在不同的地址并与不同的绑定对齐.这可能会为您提供帮助.
You basically need 2 endpoints for the same service exposed at different addresses and aligned with different bindings. This may help you.
这篇关于WCF:单个服务的多个绑定配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!