Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CallContext.LogicalGetData Vs. CallContext.GetData

Tags:

c#

.net

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?

like image 242
sternr Avatar asked May 23 '11 12:05

sternr


2 Answers

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

like image 88
wageoghe Avatar answered Oct 28 '22 23:10

wageoghe


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.

like image 39
user7116 Avatar answered Oct 28 '22 21:10

user7116