所以我在想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家伙保持为每个请求不同的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工作在多线程环境中?
发布评论