AspNetSynchronizationContextAspNetSynchronizationContext

2023-09-03 10:00:34 作者:沉默√诠释一切

尝试使用新的C#5异步模型,它令我感到诧异 AspNetSynchronizationContext 是一个内部类(以及 AspNetSynchronizationContextBase 基地)。因此,无证。但是,这是必须知道它的作用在ASP.NET code时,利用异步/等待功能。我是正确的 它的确实保证您的延续将得到同样的 HttpContext.Current 作为原始呼叫者? 它的不保证延续将在同一个线程调用者执行?

如果后者的假设是不正确的,我得到了原来的线程可以我一定要得到延续同一个线程上下文?我的意思是主/文化与线程关联和线程本地存储?这一点很重要,因为ASP.NET本地化依赖于线程的文化和我的应用程序依赖于.NET角色的安全模型(线程的本金)。

解决方案   

我是正确,它并保证您的延续将得到同样的HttpContext.Current为原来的来电?它并不能保证延续将在同一个线程调用者执行?

HttpContext.Current 是preserved,是的,在延续可能会在不同的线程中执行。

  

我的意思是与线程主体/文化相关联和线程本地存储?这一点很重要,因为ASP.NET本地化依赖于线程的文化和我的应用程序依赖于.NET角色的安全模型(线程的本金)。

普通线程本地存储丢失。您可以通过使用缓解这种 LogicalCallContext (与执行上下文流),但与异步更容易只是直接引用变量。

首席总是preserved;不这样做将是一个安全隐患。这与执行上下文流。

我认为,文化与 AspNetSynchronizationContext 流,但我还没有测试过这一点,在.NET 4.5的新实现。

您可能会发现的SynchronizationContext 我 MSDN文章的帮助。这不是官方文件(我不为微软工作),但至少它的东西。请注意, AspNetSynchronizationContext 在那篇文章现在被称为 LegacyAspNetSynchronizationContext 在.NET 4.5中引用。

另一个伟大的资源是斯蒂芬Toub的ExecutionContext与的SynchronizationContext

Trying to use new C# 5 async model it was surprising to me AspNetSynchronizationContext is an internal class (as well as AspNetSynchronizationContextBase base). Thus undocumented. But it's essential to know what it does when utilizing async/await feature within your ASP.NET code. Am I correct that It does guarantee your continuations will get the same HttpContext.Current as original callers? It does not guarantee the continuations will execute on the same thread as the callers?

If the latter assumption is not true and I get the original thread can I be sure to get the same thread context in continuations? I mean principal/culture associated with the thread and thread local storage? That's important because ASP.NET localization relies on thread's culture and my application relies on .NET role security model (thread's principal).

解决方案

Am I correct that It does guarantee your continuations will get the same HttpContext.Current as original callers? It does not guarantee the continuations will execute on the same thread as the callers?

Yes, HttpContext.Current is preserved, and yes, the continuations may execute on a different thread.

I mean principal/culture associated with the thread and thread local storage? That's important because ASP.NET localization relies on thread's culture and my application relies on .NET role security model (thread's principal).

Ordinary thread-local storage is lost. You can mitigate this by using LogicalCallContext (which flows with ExecutionContext), but with async it's easier to just reference the variables directly.

Principal is always preserved; to do otherwise would be a security risk. This flows with ExecutionContext.

I believe that culture flows with AspNetSynchronizationContext, but I haven't tested this out on .NET 4.5's new implementation.

You may find my MSDN article on SynchronizationContext helpful. It's not official documentation (I don't work for Microsoft), but at least it's something. Note that the AspNetSynchronizationContext referenced in that article is now called LegacyAspNetSynchronizationContext in .NET 4.5.

Another great resource is Stephen Toub's ExecutionContext vs. SynchronizationContext.