dragonfly
dragonfly

Reputation: 17773

CallContext in WCF

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

Thanks, Pawel

Upvotes: 7

Views: 6294

Answers (1)

Quartermeister
Quartermeister

Reputation: 59139

If you aren't using it from inspectors then it should be safe, but if you are not using Remoting or crossing an AppDomain boundary then it is probably simpler to just use a thread static field. Put a ThreadStaticAttribute on a static field and it will be a separate storage location in each thread.

If you are trying to set values in an IDispatchMessageInspector, for example, then it won't work since those will run in a separate thread from the request. Take a look at OperationContext, which will provide call-specific information about a WCF request. You can add extensions to it that can store custom data by implementing IExtension<OperationContext> and adding them to the Extensions property. Here is a blog post that describes how to add custom data to the OperationContext.

Upvotes: 8

Related Questions