.net WCF:单个服务的多个绑定配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17201656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:19:33  来源:igfitidea点击:

WCF: Multiple binding configurations for a single service

.netwcfwcf-binding

提问by John Russell

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.

我正在开发一个必须支持向后兼容性的客户端-服务器应用程序(.NET 4 WPF、WCF)。换句话说,就操作合约和数据合约而言,旧客户端应该与新服务器兼容(反之亦然)。

Our WCF services are hosted in IIS, and they wereset up to use 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>

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);
}

Recently, I discovered that we could change our encoding from XMLto binary. Combined with IIS compression, this really boosted the performance of our WCF methods such as GetData listed above.

最近,我发现我们可以将编码从 更改XMLbinary. 结合 IIS 压缩,这确实提高了我们的 WCF 方法(例如上面列出的 GetData)的性能。

This encoding change also required a change in the client and server WCF bindings, switching from a basicHttpBindingto a customBinding.

此编码更改还需要更改客户端和服务器 WCF 绑定,从 a 切换basicHttpBindingcustomBinding.

<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>

...

Here's the problem. Since our software must support client/server backwards compatibility, if an old client with the old basicHttpBindingtries 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"

这就是问题所在。由于我们的软件必须支持客户端/服务器向后兼容性,如果旧客户端basicHttpBinding尝试使用新客户端访问服务器customBinding,则调用将因不匹配而失败,例如"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?

我可以为同一个服务合同设置两个绑定配置——一个是基本的,另一个是自定义的,并且它们都指向同一个接口?我该如何解决这个问题?

采纳答案by Anu

You basically need 2 endpoints for the same service exposed at different addresses and aligned with different bindings. Thismay help you.

对于在不同地址公开并与不同绑定对齐的同一服务,您基本上需要 2 个端点。 可能对你有帮助。

回答by Dan Ling

You can have 2 two different bindings for the same service contract, but you'll need to create separate service nodes in the config and you will also need separate endpoints defined. So create a new endpoint for the binary formatted service, and have the new version of the client reference it.

对于同一个服务合同,您可以有 2 个不同的绑定,但是您需要在配置中创建单独的服务节点,并且还需要定义单独的端点。因此,为二进制格式的服务创建一个新端点,并让新版本的客户端引用它。