什么“Current”属性与ExecutionContext一起流动

8
这是一个两部分问题:
  1. Can someone provide a list of the ASP.NET/.NET properties that are typically thread local that flow with ExecutionContext?

    HttpContext.Current? Thread.CurrentContext? Thread.CurrentPrincipal? Thread.CurrentCulture?

    What properties can I count on surviving/persisting async/await?

    What else?

  2. Is there any way to add application specific Context information that will flow automatically with ExecutionContext? Something like

    var ec = ExecutionContext.Capture();
    ec.CustomContext["MyCustomContext"] = ACustomContext;
    

1
你实际上想要做什么?为什么你觉得需要存储自定义上下文? - Reed Copsey
如果你有两个不同的问题,你应该提出两个问题 - svick
@ReedCopsey,我正在尝试重构一些使用ThreadStatic和其他线程本地存储机制的遗留代码,以便它能够与async/await协同工作。 - Joe Enzminger
@JoeEnzminger,你可能想要检查一下这个链接:http://referencesource.microsoft.com/#mscorlib/system/threading/executioncontext.cs#3a777fa35a9f1ffe - noseratio - open to work
1个回答

14

感谢(LogicalSetData / LogicalGetData)。我已经阅读了您提到的每篇文章,但仍然存在一个问题,即框架上下文的流程是什么。这似乎是需要记录的重要内容... - Joe Enzminger
@JoeEnzminger:顺便说一下,我不推荐使用逻辑get/set数据。如果你找不到其他解决方案,它只是可能被使用。我没有运行基准测试,但我预计它比ThreadStatic慢得多。显式方法参数或实例字段将是我首选的解决方案。 - Stephen Cleary
1
+1. 看到实际流动的内容是很有趣的,尽管没有记录:http://referencesource.microsoft.com/#mscorlib/system/threading/executioncontext.cs#3a777fa35a9f1ffe。特别有趣的是同步上下文通过`ExecutionContext.Run`被显式地流动,但不是隐式地流动。 - noseratio - open to work
1
@Noseratio 如果您阅读了 Toub 的链接文章(特别是最后几段),您会发现 SynchrnoizationContext 实际上大部分时间不会被传播(不确定您所指的明确/隐式是什么意思)。 - Ohad Schneider
2
@OhadSchneider,我所说的显式是指显式调用ExecutionContext.CaptureExecutionContext.Run。而隐式则是指让异步/等待基础设施自动流动。 - noseratio - open to work

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接