它是安全的存储在ASP.NET线程静态变量的ObjectContext的? [英] Is it safe to store an ObjectContext in a thread static variable in ASP.NET?

查看:106
本文介绍了它是安全的存储在ASP.NET线程静态变量的ObjectContext的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过,我应该存储的的ObjectContext HttpContext.Current ,以分享我的的ObjectContext 跨越被称为一个请求不同的服务/资源库。我想知道,如果它是安全的使用的ObjectContext 上的 [ThreadStatic] 属性的静态类变量来代替。那是一个安全的事是什么?是每个请求处理在自己的线程?

I've read that I should store an ObjectContext in HttpContext.Current in order to share my ObjectContext across different services/repositories that are called in a request. I'm wondering if it is safe to use an ObjectContext with the [ThreadStatic] attribute on a static class variable instead. Is that a safe thing to do? Is each request processed in its own thread?

推荐答案

没有,也可以在同一个线程的多个请求,更重要的是,一个请求可以在多线程处理的。这就是所谓的螺纹敏捷中,你会遇到问题,当你存储的东西,在线程静态变量,而不是Context:当ASP.NET从一个线程移动到另一个的相同的请求期间的,HttpContext的是仍然可以访问,但是线程静态变量不是。

No, there can be multiple requests in the same thread and, more importantly, one request can be processed in multiple threads. This is called thread agility, and you will run into problems when you store stuff in thread-static variables instead of the Context: When ASP.NET moves from one thread to another during the same request, the HttpContext is still accessible, but the thread-static variable is not.

进一步的信息,有些链接:

Some links with further information:

  • <一个href="http://stackoverflow.com/questions/273301/callcontext-vs-threadstatic">http://stackoverflow.com/questions/273301/callcontext-vs-threadstatic (相关问题)
  • <一个href="http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html">http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html (博客条目详情)
  • http://stackoverflow.com/questions/273301/callcontext-vs-threadstatic (related question)
  • http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html (blog entry with details)

这篇关于它是安全的存储在ASP.NET线程静态变量的ObjectContext的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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