WCF 1 Session = 1 Thread?

Go To StackoverFlow.com

2

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?

2012-04-03 19:46
by Banshee


3

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
2012-04-03 20:46
by Andriy Buday
The document you referenced does not say anything about each session being executed on one thread per client. How did you reach that conclusion - David Nelson 2012-04-03 21:12
For example read it here: http://mkdot.net/mknetug/b/dejan/archive/2008/04/29/wcf-service-behaviors-instance-and-concurrency-management.asp - Andriy Buday 2012-04-03 21:16
That article is poorly worded and ultimately inaccurate. Subsequent requests on the same session can be executed on different threads. It is very easy to build a service to prove this - David Nelson 2012-04-03 21:30
Thanks! But say that a session is opened but no requests is done right now, does this still mean that this session have a thread allocated or will the thread be allocated first on the next request (like PerCall) - Banshee 2012-04-04 06:48
As I stated in my answer, using sessions does not affect the threading behavior of WCF. Each request is handled on a thread from the thread pool; if a request is not being processed, no threads are "allocated" by WCF (though of course they could remain active in the thread pool) - David Nelson 2012-04-04 14:23
David, good that we are now both on same page :) - Andriy Buday 2012-04-04 15:34


1

"If I run my WCF service(hosted in II7) and uses PerSession on contextinstance, will one session be the same as one thread?"

No, requests will be handled by the thread pool.

"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?"

No, the session is not taking a thread if there are no requests being serviced. A WCF "session" is just data; state tied to a session ID. It has nothing to do with threads.

2012-04-03 21:11
by David Nelson
When you say that request is going to be handled by thread pool isn't it the same as using Thread, even it was allocated by ThreadPool - Andriy Buday 2012-04-03 21:14
No, it is not the same. Particularly, there is no guarantee that subsequent requests on the same session will use the same thread that previous requests used; new requests will be serviced by a thread pool thread when one is available - David Nelson 2012-04-03 21:31
Hi... So I actually did not take your word and built service with sessions to verify. And you are right that another thread can be used for same session by one client. But anyway it only means that ThreadPool is effectively used. But if you start 1000 concurrent clients ThreadPool will allocate many threads, which involves resource - Andriy Buday 2012-04-03 21:45