CallContext.LogicalGetData Vs. CallContext.GetData

后端 未结 2 1016
北海茫月
北海茫月 2021-01-31 14:30

The CallContext API has LogicalGetData & GetData, but the MSDN Documentation doesn\'t do much to explain the difference between the tw

2条回答
  •  深忆病人
    2021-01-31 15:03

    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

提交回复
热议问题