同步对 ASP.NET 会话成员的访问 [英] Synchronizing Access to a member of the ASP.NET session

查看:21
本文介绍了同步对 ASP.NET 会话成员的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Javascript 应用程序,并且 eash 用户有一个单独的 UserSession.应用程序进行了一堆 Ajax 调用.每个 Ajax 调用都需要为用户访问单个 UserSession 对象.

I'm building a Javascript application and eash user has an individual UserSession. The application makes a bunch of Ajax calls. Each Ajax call needs access to a single UserSession object for the user.

  1. 每个 Ajax 调用都需要一个 UserSession 对象.

  1. Each Ajax call needs a UserSession object.

UserSession 对象中的数据对每个用户都是唯一的.

Data in the UserSession object is unique to each user.

最初,在每次 Ajax 调用期间,我都会创建一个新的 UserSession 对象,它的数据成员存储在 ASP.NET Session 中.但是,我发现 UserSession 对象被实例化了很多.为了尽量减少 UserSession 对象的构造,我将它包装在单例模式中并同步访问它.

Originally, during each Ajax call I would create a new UserSession object and it's data members were stored in the ASP.NET Session. However, I found that the UserSession object was being instantiated a lot. To minimize the construction of the UserSession object, I wrapped it in a Singleton pattern and sychronized access to it.

我相信同步正在整个应用程序范围内进行,但我只需要每个用户进行同步.我在这里看到一篇文章说 ASP.NET 缓存是同步的,但是在创建对象和将其插入缓存之间的时间另一个线程可以开始构建它是另一个对象并将其插入缓存.

I believe that the synchronization is happening application wide, however I only need it to happen per user. I saw a post here that says the ASP.NET cache is synchronized, however the time between creating the object and inserting it into the cache another Thread could start construction it's another object and insert it into the cache.

这是我当前同步访问对象的方式.有没有比使用锁定"更好的方法...应该锁定 HttpContext.Session 对象?

Here is the way I'm currently synchronizing access to the object. Is there a better way than using "lock"... should be be locking on the HttpContext.Session object?

private static object SessionLock = new object();

public static WebSession GetSession
{
    get
    {
        lock (SessionLock)
        {
            try
            {
                var context = HttpContext.Current;
                WebSession result = null;

                if (context.Session["MySession"] == null)
                {
                    result = new WebSession(context);
                    context.Session["MySession"] = result;
                }
                else
                {
                    result = (WebSession)context.Session["MySession"];
                }

                return result;
            }
            catch (Exception ex)
            {
                ex.Handle();
                return null;
            }
        }
    }
}

推荐答案

你不需要锁定 Session 状态访问.

You don't need to lock Session state access.

会话状态的物理值在完成请求所需的时间内被锁定.锁由 HTTP 模块内部管理,用于同步对会话状态的访问.

The physical values of a session state are locked for the time needed to complete a request. The lock is managed internally by the HTTP module and used to synchronize access to the session state.

http://msdn.microsoft.com/en-us/library/aa479041.aspx

这篇关于同步对 ASP.NET 会话成员的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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