【问题标题】:session variable becoming null after some time一段时间后会话变量变为空
【发布时间】:2013-01-31 05:31:36
【问题描述】:

我已经开发了一个 web 应用程序,它将执行一些图像的幻灯片放映。我有这个应用程序的 jquery ajax web 方法调用

图像滚动完美......但一段时间后它停止了......我从日志中发现我的会话变量(这里是 Session[“Username”])正在变为 null。我的代码是

   [WebMethod]
    [ScriptMethod]
    public static string GetNextImage()
    {
        try
        {

            if (HttpContext.Current.Session["Username"] != null)
            {
                string username = HttpContext.Current.Session["Username"].ToString();
                RollingScreenBAL bal = new RollingScreenBAL();
                DetailsForSlideShow[] details = bal.GetDetailsForSlideShow(username);
                int count = details.Length;
                int i;

                for (i = 0; i < count; i++)
                {
                    if (HttpContext.Current.Session["Count"] == null)
                    {

                        HttpContext.Current.Session["GraphUrl"] = details[0].GraphUrl;
                        HttpContext.Current.Session["Count"] = i + 1;
                        break;
                    }
                    else
                    {
                        if (Convert.ToInt32(HttpContext.Current.Session["Count"]) == i)
                        {
                            HttpContext.Current.Session["GraphUrl"] = details[i].GraphUrl;
                            if (i == (count - 1))
                            {
                                HttpContext.Current.Session["Count"] = null;
                            }
                            else
                            {
                                HttpContext.Current.Session["Count"] = i + 1;
                            }
                            break;

                        }
                    }


                }

            }
            return HttpContext.Current.Session["GraphUrl"].ToString();



        }
        catch (Exception ex)
        {

            ConfigurationManager configMgr = new ConfigurationManager();
            string traceFilePath = configMgr.GetTraceFilePath();
            StreamWriter traceFile = new StreamWriter(traceFilePath, true);
            traceFile.WriteLine(ex.Message + "\n" + ex.StackTrace + "Current User is  " + **HttpContext.Current.Session["Username"])**;
            traceFile.Close();
            return ex.Message;
        }
    } 

HttpContext.Current.Session["Username"]) 会话值变为空..谁能帮我为什么它会变为空?提前谢谢...

【问题讨论】:

    标签: session


    【解决方案1】:

    当会话超时(并且未重新建立/重新初始化)时,或者当您将会话字典中的特定项目显式设置为 null 时,您的会话变量变为 null。

    【讨论】:

    • 实际上图像仅在几秒钟的时间范围内滚动......但默认情况下会话值将持续 20 分钟......所以我认为会话超时不能是我的情况的原因.. ..
    猜你喜欢
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多