简体   繁体   English

在同一个WCF频道或会话上使用多个服务合同

[英]Utilizing multiple service contracts over the same WCF channel or session

I'm in the process of writing a duplex WCF service using NetTcpBinding, and I've run into an architecture question that I think I know the answer to, but hope that I'm wrong. 我正在使用NetTcpBinding编写双工WCF服务,我遇到了一个架构问题,我我知道答案,但希望我错了。

Our service is stateful, and we've selected NetTcpBinding with PerSession InstanceContextMode . 我们的服务是有状态的,我们选择了带有PerSession InstanceContextMode NetTcpBinding。 For various reasons, this is something that we require. 由于各种原因,这是我们需要的。 I'm trying to break up our larger interface (where large blocks of the operations would not apply to many clients) into multiple smaller interfaces with the operations logically grouped. 我正在尝试将我们更大的接口(其中大块操作不适用于许多客户端)分解为多个较小的接口,并将操作逻辑分组。 While it's simple enough to have a single service implementation implement all of the contracts, I'm not sure if it's possible to have multiple service contracts share a single channel (or, more to my requirement, a single session ), and I'd definitely need to be able to do that in order to make this work. 虽然简单到足以让单个服务实现实现所有合同,但我不确定是否可以让多个服务合同共享一个通道(或者,更符合我的要求,单个会话 ),而且我会肯定需要能够做到这一点,以使这项工作。

I could, of course, include everything on one contract and throw FaultException s when an invalid operation is performed, but I'd really like to be able to break these up and not even add an endpoint for inapplicable contracts. 当然,我可以在一个合同中包含所有内容,并在执行无效操作时抛出FaultException ,但我真的希望能够破解它们,甚至不为不适用的合同添加端点。 Is what I'm looking for possible? 我正在寻找什么?

TL;DR Version: TL; DR版本:

I need to be able to do this: 我需要能够做到这一点:

[ServiceContract]
public interface IServiceA
{
    [OperationContract]
    void Foo();
}

[ServiceContract]
public interface IServiceB
{
    [OperationContract]
    void Bar();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service : IServiceA, IServiceB
{
    ...
}

And be able to establish one session from the client to the service but use both IServiceA and IServiceB . 并且能够建立从客户端到服务的一个会话,但同时使用IServiceAIServiceB

The default instance provider over a sessionful channel will give you an instance per connection in your case. 会话通道上的默认实例提供程序将为您提供每个连接的实例。 You can however extend the instance provider to pick up an existing object from your own cache and return the same object. 但是,您可以扩展实例提供程序以从您自己的缓存中获取现有对象并返回相同的对象。

How you correlate instances will be upto you using some special message header etc. The underlying channel/Connection will be different for each proxy and also use differnt buffers / concurrency models but you can allow service model to use the same instance. 如何关联实例将取决于您使用一些特殊的消息头等。每个代理的底层通道/连接将不同,并且还使用不同的缓冲区/并发模型,但您可以允许服务模型使用相同的实例。 http://msdn.microsoft.com/en-us/magazine/cc163590.aspx http://msdn.microsoft.com/en-us/magazine/cc163590.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM