HttpContext.Current 在多线程环境中如何工作?

编程入门 行业动态 更新时间:2024-10-22 13:38:42
本文介绍了HttpContext.Current 在多线程环境中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

So I'm left wondering how exactly asp is able to scope a static property, when (to my knowledge) asp 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 在多线程环境中如何工作?

本文发布于:2023-10-18 13:02:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多线程   环境   工作   HttpContext   Current

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!