Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Operation.Context not Thread safe?

I'm code reviewing a WCF service. In the header of each message we inject data that the service is going to use later to build a connection string to a DB. That's because the service is going to be used by a number of different sites, each with its own DB that the service has to query. We use wcf extensibility. We have a custom MessageInspector that, after receiving the request, extracts the data from the message header, creates a context (that implements IExtension) and adds it to OperationContext.Current.Extensions. Before sending the reply the custom context is removed from the Extencions collection.

This is a fairly common pattern, as discussed here:

Where to store data for current WCF call? Is ThreadStatic safe?

and here:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/319cac66-66e8-4dfe-9a82-dfd289c9df1f/wcf-doesnt-have-session-storage-so-where-should-one-store-call-specific-data?forum=wcf

This all works fine as long as the service receives a request, processes it, sends the reply and receives the next request. But what if the service receives a request and before being able to reply it gets a second request? I built a small console application to test it. I send 2 messages from 2 different threads, I made the wcf service wait for 2 seconds, to ensure the second request comes in before the first one is completed and this is what I get:

Site Id : test1450 ; Session: uuid:2caf47cf-7d46-4d72-9275-d9c037fa0e70;id=2 : Thread Id: 6

Site Id : test1450 ; Session: uuid:2caf47cf-7d46-4d72-9275-d9c037fa0e70;id=3 : Thread Id: 22

It looks like wcf creates 2 sessions executing on 2 different threads, but Site Id is the same. It shouldn't. Judging from this it looks like OperationContext.Current.Extensions is a collection shared between threads. Right now I'm inclined to think my test is wrong and I missed something.

Has anyone tried something similar and found out that OperationContext.Current is not thread safe?

like image 483
Hoppers13 Avatar asked Oct 10 '13 09:10

Hoppers13


1 Answers

OperationContext.Current like other similar properties such as HttpContext.Current have thread affine (or thread static) values. So they are thread safe in the sense that multiple threads can read them, but different threads will get different instances. They can be thought of as dictionaries between specific threads and instances.

So in this context they are not thread safe.

Requests are served by a thread pool so concurrent requests will have different thread ids. (up to a point where the thread pool is full, then requests will be put on hold)

like image 189
aL3891 Avatar answered Oct 18 '22 23:10

aL3891