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

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

问题描述

我要建一个Javascript应用程序,eash用户都有一个单独的UserSession。该应用程序使得一堆的Ajax调用。每个Ajax调用需要为用户访问单个UserSession对象。


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


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


原来,每次Ajax调用的时候,我将创建一个新UserSession对象和它的数据成员被存储在ASP.NET会话。然而,我发现UserSession对象被实例化了很多。为了尽量减少UserSession对象的构造,我把它包在一个Singleton模式和sychronized对它的访问。

我相信同步发生广泛的应用,但是我只需要每个用户发生。我在这里看到一个帖子,说ASP.NET缓存是同步的,但创建对象,并将其插入到缓存中的另一个线程就可以开始建设它的另一个对象,并将其插入到缓存之间的时间。

下面是我目前正在同步访问对象的方式。难道还有比使用锁定......应在HttpContext.Session对象上被锁定?一个更好的办法

 私有静态对象SessionLock =新的对象();公共静态WebSession的getSession
{
    得到
    {
        锁定(SessionLock)
        {
            尝试
            {
                VAR语境= HttpContext.Current;
                WebSession结果= NULL;                如果(context.Session [MySession的] == NULL)
                {
                    结果=新WebSession(背景);
                    context.Session [MySession的] =结果;
                }
                其他
                {
                    结果=(WebSession)context.Session [MySession的];
                }                返回结果;
            }
            赶上(异常前)
            {
                ex.Handle();
                返回null;
            }
        }
    }
}


解决方案

您不需要锁定会话状态的访问。


  

的会话状态的物理值被锁定为完成一个请求所需的时间。锁定由HTTP模块内部管理和用于同步访问会话状态


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

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. Each Ajax call needs a UserSession object.

  2. Data in the UserSession object is unique to each user.

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.

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.

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;
            }
        }
    }
}

解决方案

You don't need to lock Session state access.

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天全站免登陆