在同一 WCF 通道或会话上使用多个服务契约
我正在使用 NetTcpBinding 编写双工 WCF 服务,并且遇到了一个体系结构问题,我认为我知道答案,但希望我是错的。
我们的服务是有状态的,并且我们选择了带有 PerSession
InstanceContextMode
的 NetTcpBinding。由于各种原因,这是我们所需要的。我正在尝试将较大的接口(其中大块操作不适用于许多客户端)分解为多个较小的接口,并将操作逻辑分组。虽然让单个服务实现实现所有合约很简单,但我不确定是否可以让多个服务合约共享单个通道(或者,更符合我的要求,单个会话),我肯定需要能够做到这一点才能使这项工作成功。
当然,我可以将所有内容包含在一个合约中,并在执行无效操作时抛出 FaultException
,但我真的希望能够将这些分解,甚至不为不适用的情况添加端点合同。我正在寻找的可能吗?
TL;DR 版本:
我需要能够做到这一点:
[ServiceContract]
public interface IServiceA
{
[OperationContract]
void Foo();
}
[ServiceContract]
public interface IServiceB
{
[OperationContract]
void Bar();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service : IServiceA, IServiceB
{
...
}
并且能够建立从客户端到服务的一个会话,但同时使用IServiceA和<代码>IServiceB。
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.
Our service is stateful, and we've selected NetTcpBinding with PerSession
InstanceContextMode
. 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. Is what I'm looking for possible?
TL;DR Version:
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会话通道上的默认实例提供程序将为您的情况下的每个连接提供一个实例。不过,您可以扩展实例提供程序来选择从您自己的缓存中提取现有对象并返回相同的对象。
如何关联实例将取决于您使用一些特殊的消息头等。每个代理的底层通道/连接将有所不同,并且还使用不同的缓冲区/并发模型,但您可以允许服务模型使用相同的实例。
http://msdn.microsoft.com/en-us/magazine/cc163590.aspx
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