AspNet同步上下文 [英] AspNetSynchronizationContext

查看:24
本文介绍了AspNet同步上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用新的 C# 5 异步模型让我感到惊讶 AspNetSynchronizationContext 是一个内部类(以及 AspNetSynchronizationContextBase 基础).因此无证.但了解在 ASP.NET 代码中使用 async/await 功能时它的作用很重要.我是否正确它确实保证您的延续将获得与原始调用者相同的 HttpContext.Current 吗?它保证延续将在与调用者相同的线程上执行吗?

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?

如果后一个假设不正确并且我得到原始线程,我是否可以确保在延续中获得相同的线程上下文?我的意思是与线程和线程本地存储相关的主体/文化?这很重要,因为 ASP.NET 本地化依赖于线程的文化,而我的应用程序依赖于 .NET 角色安全模型(线程的主体).

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).

推荐答案

它确实保证您的延续将获得与原始调用者相同的 HttpContext.Current 我是否正确?它不保证延续将在与调用者相同的线程上执行?

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?

是的,HttpContext.Current 被保留了,是的,延续可以在不同的线程上执行.

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

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

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).

普通线程本地存储丢失.您可以通过使用 LogicalCallContext(与 ExecutionContext 一起流动)来缓解这种情况,但使用 async 直接引用变量会更容易.

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.

主体总是被保留;否则会带来安全风险.这与 ExecutionContext 一起流动.

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

我相信文化会随着 AspNetSynchronizationContext 流动,但我还没有在 .NET 4.5 的新实现.

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

您可以找到我的 关于 SynchronizationContext 的 MSDN 文章 有帮助.这不是官方文档(我不为 Microsoft 工作),但至少它是一些东西.请注意,该文章中引用的 AspNetSynchronizationContext 在 .NET 4.5 中现在称为 LegacyAspNetSynchronizationContext.

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.

另一个很棒的资源是 Stephen Toub 的 ExecutionContext 对比 SynchronizationContext.

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

这篇关于AspNet同步上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆