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

查看:481
本文介绍了如何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家伙保持为每个请求不同的AppDomain ...但似乎并不可行。

  • 另一种理论认为,该.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 :-/

推荐答案

什么马克说是最简单最有可能为你所追求的,但实际上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.

因此​​,解决办法真的是不是所有的pretty可悲的是,那里的threadstatic要简单得多,可能需要适合的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天全站免登陆