none
WCF,Streaming and Sessions

    Question

  • Hello.

    I'm writing service, which recieves large files with some info and saves files to disk, info to database. TransferMode=Streamed.

    I have to use design with two methods. First method gets info about file, and second method gets file stream. InstanceContextMode=PerSession.

    From MSDN:
    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.

    And I can't understand, if ConcurrencyMode=Multiple, InstanceContextMode=PerSession, TransferMode=Streamed - how service will behave when several users will transfer files simultaneously. Will new instance of class created for each user (and his session), and I can store some data in class fields, or it will be only one service class instance for all users, and request from each user will be handled in different threads but by the same class instance (so it is unreasonably to store data (some file info) in class fields... Or maybe it is possible to use Thread-local storage?..)

    I have read http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/aac8ba08-d779-4a24-bf3a-dc19a00d39f1/ and http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/590a2d3b-955f-4f6c-ac24-84cccc194891/.

    Is it possible to use netTcpBinding? How to configure it? If SessionMode=Required i'm getting an exception "contract needs Session property, but NetTcpBinding is not supports it or configured incorrectly" (in russian "Контракту требуется свойство Session, однако привязка NetTcpBinding его не поддерживает или этого не позволяет неправильная настройка"). And when I add to configuration <reliableSession enabled="true"/>, exception is "Transfer mode Streamed is not supported by ReliableSessionBindingElement".

    What should I do?

    Thank  you.

     

     

     

    Monday, October 12, 2009 4:38 PM

Answers

  • Hi,

    As Bin said you cannot use RM if you are using streaming. And you cannot use streaming and session together as you seen from the threads that you have mentioned. Try explicitly setting the InstanceContextMode to Percall. This is from MSDN :

    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.

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

    Please find below another thread about TCP binding with streaming and session

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/590a2d3b-955f-4f6c-ac24-84cccc194891/

     

    Please find below a thread with NetTcpBinding and STreaming working sample

    http://social.msdn.microsoft.com/forums/en-US/wcf/thread/b878e2a4-c0c8-417e-8e91-887ef1f39a28

    • Marked as answer by Bin-ze Zhao Monday, October 19, 2009 2:52 AM
    Sunday, October 18, 2009 3:06 AM
  • Hi,

    Only the BasicHttpBinding, NetTcpBinding, and NetNamedPipeBinding support streaming.

    Also you cannot use streaming when using reliable messaging (WS-RM). This is because WS-RM needs to apply signing/checksums to the whole message as a unity, etc; and this is not possible when streamed transferMode, only with buffered transferMode.

    But this is nothing to do with session, as per session and reliable session are two completely different concept. Reliable session implements the WS-RM specification, is useful for ensuring reliable, in order delivery of messages between endpoints over intermediaries.

    However netTcpBinding does support transport session. For how to use session please refer to:
    http://msdn.microsoft.com/en-us/library/ms733040.aspx

    Thanks
    Binze


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, October 19, 2009 2:52 AM
    Friday, October 16, 2009 8:22 AM

All replies

  • Hi,

    Only the BasicHttpBinding, NetTcpBinding, and NetNamedPipeBinding support streaming.

    Also you cannot use streaming when using reliable messaging (WS-RM). This is because WS-RM needs to apply signing/checksums to the whole message as a unity, etc; and this is not possible when streamed transferMode, only with buffered transferMode.

    But this is nothing to do with session, as per session and reliable session are two completely different concept. Reliable session implements the WS-RM specification, is useful for ensuring reliable, in order delivery of messages between endpoints over intermediaries.

    However netTcpBinding does support transport session. For how to use session please refer to:
    http://msdn.microsoft.com/en-us/library/ms733040.aspx

    Thanks
    Binze


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, October 19, 2009 2:52 AM
    Friday, October 16, 2009 8:22 AM
  • Thank you. 

    http://msdn.microsoft.com/en-us/library/ms733040.aspx:
    > A client starts a session with a service endpoint by calling one of the following:
    > An initiating operation on either type of WCF client object (by default, all operations are initiating). When the first operation is called, the WCF client object automatically opens the channel and initiates a session.

    Could you provide any sample with Session and Streaming?

    Why does not this code work?

    Imports System.Runtime.Serialization
    Imports System.ServiceModel
    
    Module SessionStrreamingService
    
        <ServiceContract()> Interface ISSS
            <OperationContract()> Sub Store(ByVal s As String)
            <OperationContract()> Function Restore() As String
        End Interface
    
        <ServiceBehavior(AutomaticSessionShutdown:=False)> Class SSS
            Implements ISSS
    
            Private Storage As String
    
            Public Function Restore() As String Implements ISSS.Restore
                Console.WriteLine(" serv{0} Restore", Me.GetHashCode)
                Return Storage
            End Function
    
            Public Sub Store(ByVal s As String) Implements ISSS.Store
                Console.WriteLine(" serv{0} Store", Me.GetHashCode)
                Storage = s
            End Sub
        End Class
    
        Sub Main()
            Using h = New ServiceHost(GetType(SSS))
                Dim b = New NetTcpBinding
                b.TransferMode = TransferMode.Streamed ' i need this for file transfer
                Const ep As String = "net.tcp://localhost:8000/sss"
                h.AddServiceEndpoint(GetType(ISSS), b, ep)
                h.Open()
                Console.WriteLine("server started")
    
                Using cf = New ChannelFactory(Of ISSS)(b, ep)
                    Dim c = cf.CreateChannel()
                    Dim testStore = "store me"
                    Console.WriteLine("stored: {0}", testStore)
                    c.Store(testStore)
                    testStore = c.Restore()
                    Console.WriteLine("restored: {0}", testStore)
                End Using
    
            End Using
    
    
            Console.WriteLine("press any key to exit")
            Console.ReadKey()
        End Sub
    
    End Module
    
    Saturday, October 17, 2009 8:25 PM
  • Hi,

    As Bin said you cannot use RM if you are using streaming. And you cannot use streaming and session together as you seen from the threads that you have mentioned. Try explicitly setting the InstanceContextMode to Percall. This is from MSDN :

    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.

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

    Please find below another thread about TCP binding with streaming and session

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/590a2d3b-955f-4f6c-ac24-84cccc194891/

     

    Please find below a thread with NetTcpBinding and STreaming working sample

    http://social.msdn.microsoft.com/forums/en-US/wcf/thread/b878e2a4-c0c8-417e-8e91-887ef1f39a28

    • Marked as answer by Bin-ze Zhao Monday, October 19, 2009 2:52 AM
    Sunday, October 18, 2009 3:06 AM