CallContext in WCF

It is safe to use CallContext when a request arrives at the WCF service, initialize it with some specific data (for example, using the hook at the beginning of the call: Inspector / ContextBoundObject), and then reuse it in the call and ensure that the data I access is all the time the same data?

Thanks Pawel

+7
multithreading c # wcf
source share
1 answer

If you do not use it with inspectors, this should be safe, but if you do not use Remoting or cross the border of the AppDomain, then it is probably easier to just use a static stream field. Put a ThreadStaticAttribute in a static field and this will be a separate storage location in each thread.

If you are trying to set values ​​in IDispatchMessageInspector , for example, then this will not work, as they will be executed in a separate thread from the request. See OperationContext for information on a specific WCF request call. You can add extensions to it that can store user data by implementing IExtension<OperationContext> and adding them to the Extensions property. Here is a blog post that describes how to add user data to an OperationContext.

+7
source share

All Articles