7

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

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343

1 Answers1

4

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

Sajay
  • 444
  • 2
  • 6
  • Interesting...that's definitely a good find, even if it doesn't quite do what I want, which is to share a connection (I don't want to have to open up one port in the firewall per service contract). Is there any way to accomplish that? – Adam Robinson Oct 22 '11 at 00:07
  • You can have a common root base address like net.tcp://test.com:8000/Myservice and have relative addresses below it per endpoing like net.tcp://test.com:8000/Myservice/Contract1 net.tcp://test.com:8000/Myservice/Contract2 etc. So you have only one port but the connection demuxer will delegate to the appropriate contract. You cannot however share the same connection for different proxy instances. – Sajay Oct 22 '11 at 00:22
  • I'm OK with different connections so long as there's only one port in use. This is great; thanks! – Adam Robinson Oct 22 '11 at 00:27