NetTcpBinding with Streaming and Session
Asked Answered
U

1

7

I’m trying to set up a WcfService with the use of NetTcpBinding. I use Transfer mode Streamed since I will transfer large files. I need to use Session, and I have read that NetTcpBinding supports this, but when I turn it on like:

SessionMode=SessionMode.Required

I get the error:

System.InvalidOperationException: Contract requires Session, but Binding 'NetTcpBinding' doesn't support it or isn't configured properly to support it.

Does anyone know what I have to do to make NetTcpBinding work with sessions? Thanks for any help :)

Ulterior answered 5/1, 2010 at 9:18 Comment(0)
B
13

You've no doubt solved this - but for others that come across it (as I did)...

According to "Programming WCF Services" by Juval Lowy - you can't use streaming with a contract that is configured SessionMode.Required. See page 243

Neither can you use NetTcpBinding with reliable messaging with streaming.

It doesn't elaborate as to why.

One workaround might be to split the operations that require session mode into a separate contract and the streaming operations into another. Then implement a unique ID for each client (unique GUID for the lifetime of the client app) which is passed in the non-streaming interface as a RegisterSession(Guid mySessionId) operation. When sessions are created on the server - they can register with a session manager object which stores the GUID, SessionContractImplemenation pair in a Dictionary.

Then add a param to the streaming contract operation (same GUID) so that the streaming contract implementation can access the live non-streaming object (via the session manager you created - using the GUID provided).

You'll have to manage session lifetimes appropriately of course.

From Microsoft...

Sessions and Streaming When you have a large amount of data to transfer, the streaming transfer mode in WCF is a feasible alternative to the default behavior of buffering and processing messages in memory in their entirety. You may get unexpected behavior when streaming calls with a session-based binding. All streaming calls are made through a single channel (the datagram channel) that does not support sessions even if the binding being used is configured to use sessions. If multiple clients make streaming calls to the same service object over a session-based binding, and the service object's concurrency mode is set to single and its instance context mode is set to PerSession, all calls must go through the datagram channel and so only one call is processed at a time. One or more clients may then time out. You can work around this issue by either setting the service object's InstanceContextMode to PerCall or Concurrency to multiple.

Note:
MaxConcurrentSessions have no effect in this case because there is only one "session" available.

See http://msdn.microsoft.com/en-us/library/ms733040.aspx

Billie answered 16/3, 2010 at 9:19 Comment(1)
How can I add a GUID param to the streaming contract? I get an InvalidOperationException: For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream.Amative

© 2022 - 2024 — McMap. All rights reserved.