会话变量在一段时间后变为null

时间:2013-01-31 05:31:36

标签: session

我开发了一个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”])会话值变为null ..任何人都可以帮助我为什么它会变为null?提前谢谢......

1 个答案:

答案 0 :(得分:0)

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