The CallContext
API has LogicalGetData
& GetData
, but the MSDN Documentation doesn't do much to explain the difference between the two, and when they differ.
Any ideas?
Generally, data stored via CallContext.SetData
is considered to be thread local. That is, any call to CallContext.GetData
will get the data that was set via SetData from the same thread. Data stored via CallContext.LogicalSetData
is considered to be "logical thread" local. That is, any data that is stored via CallContext.LogicalSetData
will be "flowed" to any child threads. If you call CallContext.LogicalGetData
in the same thread or any child threads, you will get the data that was stored by that thread's (or the parent thread's) call to CallContext.LogicalSetData
.
As @sixlettervariables points out, there are also some specific differences related to Remoting and cross AppDomain calls (maybe cross AppDomain implies Remoting, I don't know, I am not that familiar with Remoting in general).
Also as pointed out by @sixlettervariables, by implementing the marker interface ILogicalThreadAffinative on an object and then storing that object using CallContext.SetData
, the object will essentially behave as if it had been stored by CallContext.LogicalSetData
.
Here is a good blog posting from Jeff Richter about using LogicalSetData/LogicalGetData:
http://www.wintellect.com/CS/blogs/jeffreyr/archive/2010/09/27/logical-call-context-flowing-data-across-threads-appdomains-and-processes.aspx
Here are some more links from here on SO that might shed some more light on CallContext.SetData/GetData, CallContext.LogicalSetData/LogicalGetData, and various forms of thread local storage:
CallContext vs ThreadStatic
How to Pass a variable to another Thread
It appears that this is a subtle difference related to method calls made remotely to another AppDomain
. In this instance a LogicalCallContext
is created and the data is stored in a manner accessible to LogicalGetData
. While in normal, non-remoted method calls the data is stored in a manner accessible to GetData
.
When a remote method call is made to an object in another AppDomain, the CallContext class generates a LogicalCallContext instance that travels along with the remote call. Only objects that expose the ILogicalThreadAffinative interface and are stored in the CallContext are propagated outside the AppDomain in a LogicalCallContext. Objects that do not support this interface are not transmitted in LogicalCallContext instances with remote method calls.
GetData
:
Retrieves an object with the specified name from the
CallContext
.
LogicalGetData
:
Retrieves an object with the specified name from the logical call context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With