HttpContext.Current 在多线程环境中如何工作? [英] How does HttpContext.Current work in a multi-threaded environment?

查看:24
本文介绍了HttpContext.Current 在多线程环境中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道当(据我所知)asp.net 是多线程时,asp.net 究竟如何能够确定静态属性的范围.

So I'm left wondering how exactly asp.net is able to scope a static property, when (to my knowledge) asp.net is multi-threaded.

  • 一种理论认为 ASP.NET 人员为每个请求维护不同的应用程序域……但这似乎不可行.
  • 另一种理论认为 .Current 方法查看当前线程,然后使用它在某个哈希表(或其他静态存储机制)中查找 http 上下文.

无论哪种方式,它都是一种看起来非常有用的技术......我想使用它,但绝对不想调试共享状态错误:-/

Either way, it's a technique that seems really useful ... I'd like to utilize it, but definitely don't want to be debugging shared state bugs :-/

推荐答案

Marc 所说的最简单最有可能满足您的需求,但是 ASP.NET 实际上比 ThreadStatic 所说的要复杂一些,因为实际上单个请求可以由多个线程处理..我相信 ASP.NET 发生的事情是明确地告诉执行线程切换上下文,当然托管环境正在调度线程并且它具有 httpcontext 需要执行的上下文,所以它找到一个线程,告诉线程它应该在哪个上下文中运行......然后将它发送出去.

What Marc says is the easiest most likely for what you are after, however ASP.NET is actually somewhat more complicated than what say ThreadStatic does, because single requests actually can be processed by multiple threads.. what I believe happens with ASP.NET is that the executing thread explicitely is told to switch context, of course the hosting environment is scheduling the threads and it has context of which httpcontext needs executing, so it finds a thread, tells the thread which context it should run in.. then sends it off on its way.

所以解决方案真的不是那么令人遗憾,因为线程静态要简单得多,并且可能适合 95% 的时间.

So the solution really isn't all that pretty sadly, where as threadstatic is much simpler and probably suits needs 95% of the time.

这篇关于HttpContext.Current 在多线程环境中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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