views:

41

answers:

1
+2  Q: 

CallContext in WCF

Hi,

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

+1  A: 

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.

Quartermeister
Thanks.I have designed my solution using AOP It should be safe then?
dragonfly
Another thing that bothers me...In asp .net web application it's said that one request can be server using more than one thread (something like threds switching?). It's not the same in WCF host environment? That when operation is processed in can he suspended a any point, and then resumed using another thread?
dragonfly