为什么逻辑调用上下文不跨线程传播? [英] Why is the logical call context not propagated across threads?
本文介绍了为什么逻辑调用上下文不跨线程传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个奇怪的问题,逻辑调用上下文没有传播到应用程序中的线程.
I have a weird problem with logical call contexts not propagating to threads in my application.
在此示例中,线程(在设置逻辑调用上下文数据之前创建的)不会获取新值:
In this sample, the thread (created before logical call context data is set) does not pick up the new value:
class Program
{
static void Main(string[] args)
{
var dispatcher = new MessageDispatcher();
//Logical call context data not set yet
Console.WriteLine($"Main: {Thread.CurrentThread.ManagedThreadId}: {CallContext.LogicalGetData("myvar")}"); // Main logical call context data is not set yet = Null
dispatcher.Broadcast("a string"); // logical call context data is still not set yet = Null
//Logical call context data is set now
CallContext.LogicalSetData("myvar", "Logical call context variable");
Console.WriteLine($"Main: {Thread.CurrentThread.ManagedThreadId}: {CallContext.LogicalGetData("myvar")}"); // Main logical call context data is set = return value
dispatcher.Broadcast("a string"); // Logical call context data is set and should return the data set above?
Console.ReadLine();
}
public class MessageDispatcher
{
private readonly ConcurrentQueue<string> _messages;
private readonly AutoResetEvent _waitHandle;
private bool _terminate;
private bool _messageMonitoringActive;
public MessageDispatcher()
{
_messages = new ConcurrentQueue<string>();
_waitHandle = new AutoResetEvent(false);
var thread = new Thread(MonitorCollection);
thread.Start();
}
public void Broadcast(string message)
{
_messages.Enqueue(message);
_waitHandle.Set();
}
private void MonitorCollection()
{
_waitHandle.WaitOne();
string message;
while (_messages.TryDequeue(out message))
{
Console.WriteLine(
$"{message}: {Thread.CurrentThread.ManagedThreadId}: {CallContext.LogicalGetData("myvar")}");
}
}
}
}
对于在创建的线程内进行的所有调用,未从刚刚设置的上下文变量中采用调用上下文数据.为什么会这样?
For all calls made within the created thread, the call context data is not adopted from the just set context variable. Why is that?
在我的应用程序中,我无法在实例化时设置逻辑调用上下文变量(这是在启动时通过IoC(它是WebAPI堆栈)通过IoC完成的),而只能在每次调用时进行.
In my application, I can't set logical call context variable at instantiation (which is done via IoC on startup (it's a WebAPI stack)), but only on each invocation.
推荐答案
查看全文