HttpContext Class and its Thread Safety

左心房为你撑大大i 提交于 2019-12-04 18:00:44

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 is not an instance member, it's a static member. So it's threadsafe. And it returns an HttpContext instance, but it returns a separate instance for each request -- there can be multiple threads processing requests, but each request will get a separate 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!