开发者

HttpContext Class and its Thread Safety

开发者 https://www.devze.com 2023-04-07 17:55 出处:网络
I have an Singleton object in application that has following property: private AllocationActionsCollection AllocationActions

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. 开发者_StackOverflow中文版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


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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号