I have a legacy WCF web service that uses BasicHttpBinding
and is hosted in IIS.
The web service Operation methods use ThreadStatic
fields to hold state data that is used during the processing of the request.
I want to clean up these ThreadStatic fields when each Operation is completed, so that values don't "leak" to code that may subsequently use the same thread pool thread.
I naively thought I would be able to do this in Application_EndRequest
in global.asax; but that doesn't work as it seems it runs on a different thread.
So I'm considering an alternative approach, which is to use an IDispatchMessageInspector
, and clean up the ThreadStatic
values in its BeforeSendReply
method.
Before doing so, I would like to know if it is guaranteed that BeforeSendReply
will be executed on the same thread as the Operation
method. Can anyone tell me if this is guaranteed, and if so point to an authoritative reference that confirms it?