Search code examples
.netwcfsession

WCF 1 Session = 1 Thread?


If I run my WCF service(hosted in II7) and uses PerSession on contextinstance, will one session be the same as one thread? What happens while the client is not making any calls but do have a session on the service? Is the session still taking a thread?

If I change to PerCall I supose that I will get a thread per call and that this thread is returned when the call is over.

Where can I find this information?


Solution

  • I believe you can find answers and good explanations here.

    At glance you would use PerCall for scalability reasons and PerSession for usual web scenarios.

    • When using PerSession once client did first call instance of service implementation will be kept on server. Every client has it's own session executed only on one thread (!) per client. So, yes 1 Session == 1 Thread by default. But you can also change ConcurrencyMode, so within one session client can do many concurrent calls.

    • In case of PerCall service instance will be disposed immediately after call is done.

    [EDITED (after discussions with David Nelson)]:

    (!) It doesn't mean the same thread! It only means that ThreadPool will use available thread to run service code. But if you start 1000 concurrent clients ThreadPool will allocate many threads, which involves resources, such as memory.

    Explanation of threads usage with code:

    I created simple Calculator service to show how treading works for WCF service.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class CalculatorService : ICalculatorService
    {
        private int _threadIdOnCreating = Thread.CurrentThread.ManagedThreadId;
        public int AccumulatedValue { get; private set; }
        public int Accumulate(int valueToAdd)
        {
            AccumulatedValue += valueToAdd;
    
            Console.WriteLine(
    string.Format("Accumulated: {0}. ThreadIdOnServiceCreating:{1} CurrentThreadId:{2}",
                AccumulatedValue, _threadIdOnCreating, Thread.CurrentThread.ManagedThreadId));
    
            return AccumulatedValue;
        }
    }
    

    I run Accumulate method with parameter 2 for five times and then created new client proxy and did the same. Below is output, which proves that server keeps instance of service implementation (threadId on creation), but methods are run on different threads, taken from ThreadPool.

    I'm calculator
    Accumulated: 2. ThreadIdOnServiceCreating:6 CurrentThreadId:6
    Accumulated: 4. ThreadIdOnServiceCreating:6 CurrentThreadId:7
    Accumulated: 6. ThreadIdOnServiceCreating:6 CurrentThreadId:6
    Accumulated: 8. ThreadIdOnServiceCreating:6 CurrentThreadId:7
    Accumulated: 10. ThreadIdOnServiceCreating:6 CurrentThreadId:6
    Accumulated: 2. ThreadIdOnServiceCreating:9 CurrentThreadId:9
    Accumulated: 4. ThreadIdOnServiceCreating:9 CurrentThreadId:6
    Accumulated: 6. ThreadIdOnServiceCreating:9 CurrentThreadId:9
    Accumulated: 8. ThreadIdOnServiceCreating:9 CurrentThreadId:6
    Accumulated: 10. ThreadIdOnServiceCreating:9 CurrentThreadId:8