views:

64

answers:

2

I've service which is marked with the ServiceBehavior attribute

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single,
 ConcurrencyMode = ConcurrencyMode.Multiple)]

Is there any chance for the operations in this service to be executed by same thread?

I'm storing an important data in ThreadLocal variable, which is to be used for single execution of an operation.

+3  A: 

No,

Each request is answered in a different thread. the InstanceContextMode.Single attribute specifies that the service uses the same object (and its members) for each request.

Gilad
For clarity, each request/thread is queued to use that same object
Henk Holterman
+1  A: 

Be aware that threads in WCF are handled by ThreadPool => Thread is not disposed after request processing but returned to pool so it can be reused for other request processing. I'm not sure how this works with ThreadLocal<T> but with ThreadStaticAttribute this can easily pass the value from one request processing to next random request processing which will use the same Thread.

Ladislav Mrnka
Okay, but a thread can't be used to execute more than one operation at the same time. So I'm safe if I delete the ThreadStatic/ThreadLocal<> variable after each operation?
Sandeep
I don't know about any thread switching in WCF so I say yes you should be safe. But if you find some strange non deterministic behavior in your application this should be first area you check.
Ladislav Mrnka
Actually there can be perhaps thread switching in asyncronous service operations but I think you don't use them: http://msdn.microsoft.com/en-us/library/ms734701.aspx
Ladislav Mrnka