HttpContext 类及其线程安全

发布于 2024-12-07 04:51:20 字数 1229 浏览 5 评论 0原文

我在应用程序中有一个具有以下属性的 Singleton 对象:

private AllocationActionsCollection AllocationActions
{
    get
    {
        return HttpContext.Current.Session["AllocationOptions.AllocationActions"] as AllocationActionsCollection;
    }
    set
    {
        HttpContext.Current.Session["AllocationOptions.AllocationActions"] = value;
    }
}

我正在处理一个错误(HttpContext.Current.Session["AllocationOptions.AllocationActions"] 为 null,即使对我来说它应该始终设置为有效实例...)。我刚刚在 MSDN 中读到 HttpContext 实例成员不能保证线程安全!我想知道这是否是问题所在。应用程序中的某个地方可能会出现资源竞争,并且 HttpContext.Current.Session["AllocationOptions.AllocationActions"] 为 null 的时刻就是使用此语句使用 AllocationActions setter 的时刻:

AllocationActions = new AllocationActionsCollection(Instance.CacheId);

我的问题是:

a)令我震惊的是 HttpContext.Current.Session 不是线程安全的。那么如何安全地使用该财产呢? b) 你知道为什么 Session 变量可以为空吗(尽管我很确定我在第一次使用它之前设置了它)?

谢谢,Pawel

编辑1:

a)使用以下语句每2分钟设置一次初始化会话变量的行(在Page_Load中执行)

AllocationActions = new AllocationActionsCollection(Instance.CacheId);

b)在事件处理程序中调用调用getter的代码(如Button_Click)

c)应用程序中没有自定义线程。仅常见的 HTTP 处理程序

I have an Singleton object in application that has following property:

private AllocationActionsCollection AllocationActions
{
    get
    {
        return HttpContext.Current.Session["AllocationOptions.AllocationActions"] as AllocationActionsCollection;
    }
    set
    {
        HttpContext.Current.Session["AllocationOptions.AllocationActions"] = value;
    }
}

I'm dealing with one error (HttpContext.Current.Session["AllocationOptions.AllocationActions"] is null even though it is supposed to me always set to valid instance...). I just read in MSDN that HttpContext instance member are not guaranteed to be thread safe! I wonder if that could be the issue. There could be a resources race somewhere in application and the moment where HttpContext.Current.Session["AllocationOptions.AllocationActions"] is null is the moment where AllocationActions setter is used using this statement:

AllocationActions = new AllocationActionsCollection(Instance.CacheId);

My questions are:

a) I'm shocked that HttpContext.Current.Session is not thread safe. How to safely use that property then?
b) do you have any ideas why that Session variable can be null (even though I'm pretty sure I'm setting it before it's used for the first time)?

Thanks,Pawel

EDIT 1:

a) line that initializes session variable is set every 2 minutes with following statement (executed in Page_Load)

AllocationActions = new AllocationActionsCollection(Instance.CacheId);

b) code that calls getter is called in event handlers (like Button_Click)

c) there is not custom threading in application. only common HTTP Handler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓬勃野心 2024-12-14 04:51:20

单例对象是通过将类的实例化限制为一个对象来实现的。

HttpContext.Current.Session 是一个专用于单个用户的区域;存储在会话中的任何对象仅对创建它的用户/会话可用。

存储在 Application 中的任何对象仅适用于每个用户/会话。

任何静态对象也只能供每个用户/会话使用。 建议的实现总是使用静态对象..为什么不呢?

A singleton object is realized by restricting the instantiation of a class to one object.

HttpContext.Current.Session is an area dedicated to a single user; any object stored in Session will be available only for the user/session that created it.

Any object stored in Application will be available only for every user/session.

Any static object also, will be available only for every user/session. Suggested implementations always use static objects.. why didn't you?

何其悲哀 2024-12-14 04:51:20

要安全地访问会话属性,您只需将访问包装在锁定语句中并使用会话类的 SyncRoot 对象。

To access the session property safely you would just wrap the access in a lock statement and use the SyncRoot object of the session class.

聚集的泪 2024-12-14 04:51:20

HttpContext.Current 为每个请求返回一个单独的 HttpContext 实例。可以有多个线程处理请求,但每个请求都会获得自己的 HttpContext 实例。因此,任何给定的实例都不会被多个线程使用,并且线程安全不是问题。

因此,除非您为单个请求手动启动自己的多个线程,否则您就是线程安全的。

HttpContext.Current returns a separate HttpContext instance for each request. There can be multiple threads processing requests, but each request will get its own HttpContext instance. So any given instance is not being used by multiple threads, and thread safety is not an issue.

So unless you're manually spinning up multiple threads of your own for a single request, you are threadsafe.

剧终人散尽 2024-12-14 04:51:20

HttpContext 类的线程安全性对于 .NET 来说是相当标准的。基本经验法则(除非明确指定)是静态成员是线程安全的,而实例成员不是。

无论如何,如果不深入研究设置/重置会话变量的代码,就很难判断为什么会话变量为空。或者,您可能是从与您设置的会话不同的会话中调用 get_AllocationActions 方法。同样,更多代码会有所帮助。

The thread safety of the HttpContext class is pretty standard for .NET. The basic rule of the thumb (unless explicitly specified) is that static members are thread safe and instance members aren't.

In any case it is hard to tell why your session variable is null without looking more into the code that sets/resets it. Or perhaps you are calling your get_AllocationActions method from a different session than the one you set it in. Again, more code would help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文