title: WCF Operation.Context not Thread safe?
body: <p>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.</p>
  
  <p>This is a fairly common pattern, as discussed here:</p>
  
  <p><a href="https://stackoverflow.com/questions/1895732/where-to-store-data-for-current-wcf-call-is-threadstatic-safe">Where to store data for current WCF call? Is ThreadStatic safe?</a></p>
  
  <p>and here:</p>
  
  <p><a href="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" rel="nofollow noreferrer">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</a></p>
  
  <p>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:</p>
  
  <p>Site Id : test1450 ; Session: uuid:2caf47cf-7d46-4d72-9275-d9c037fa0e70;id=2 : Thread Id: 6</p>
  
  <p>Site Id : test1450 ; Session: uuid:2caf47cf-7d46-4d72-9275-d9c037fa0e70;id=3 : Thread Id: 22 </p>
  
  <p>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.</p>
  
  <p>Has anyone tried something similar and found out that OperationContext.Current is not thread safe?</p>