【问题标题】:C# ASP.NET Session Manager throwing null reference exceptionC# ASP.NET 会话管理器抛出空引用异常
【发布时间】:2016-08-17 10:58:21
【问题描述】:

我正在尝试实现一个静态SessionManager 类,该类应该充当SessionStore 对象的包装器,该对象存储在HttpContext.Current.Session["objSession"] 中,实际上包含所有用户的会话数据。 SessionManager 类具有与SessionStore 相同的属性,但具有根据需要操作会话数据所需的额外方法。基本上,SessionManager 有助于获取/设置存储在会话对象中的属性。

所有类都存储在与 Web 解决方案相同的命名空间中,并且都是可序列化的。

我尝试了两种不同的解决方案来解决我的问题,当尝试对 HttpContext.Current.Session 执行任何操作时,都在同一点引发了空引用异常:

public static class SessionManager
    {
        static SessionManager()
        {
            if (HttpContext.Current.Session != null)
            {
                try
                {
                    if (HttpContext.Current.Session["objStore"] == null)
                    {
                        HttpContext.Current.Session["objStore"] = new SessionStore();
                    }
                }
                catch (NullReferenceException)
                {
                    HttpContext.Current.Session["objStore"] = new SessionStore();
                }
            }
        }

调用页面的代码隐藏:

protected void Page_Load(object sender, EventArgs e)
        {

            if (SessionManager.groupSettings.Count > 0)
            {
                pnlDashboard.Visible = true;
                pnlLogin.Visible = false;
                getDisplayData();
            }
            else
            {
                pnlDashboard.Visible = false;
                pnlLogin.Visible = true;
            }
        }

调试器进入SessionManager 一直到行

if (HttpContext.Current.Session != null)

然后它停止并抛出异常。但是,当我将鼠标悬停在代码上并打开属性对话框时,它显示 HttpContext.Current.Session 对象不为空。生成的调用堆栈在这里,但表明源代码行是if (SessionManager.groupSettings.Count > 0),在代码隐藏中:

[NullReferenceException: Object reference not set to an instance of an object.]
   Project.Default.Page_Load(Object sender, EventArgs e) in C:\Users\ASP\Project\Project\Default.aspx.cs:20
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952

我的第二次尝试将所有代码都包含在上面的静态构造函数中,但将其放在 public static sessionStart() 方法中,该方法在调用页面的代码隐藏中的第一个 if 语句之上调用:

protected void Page_Load(object sender, EventArgs e)
    {
        SessionManager.sessionStart()
        if (SessionManager.groupSettings.Count > 0)
        {
            pnlDashboard.Visible = true;
            pnlLogin.Visible = false;
            getDisplayData();
        }
        else
        {
            pnlDashboard.Visible = false;
            pnlLogin.Visible = true;
        }
    }

我真的很难理解是什么导致了这个问题。我的代码中其他地方有静态类并且没有任何问题,而且 Session 似乎不是空的。

感谢所有帮助。谢谢!

【问题讨论】:

  • SessionManager.groupSettings?那是什么?
  • A List<GroupSettings> 实际上似乎是我的问题的原因...我初始化了 SessionStore 中的所有属性,似乎问题已解决(也许它无法序列化空对象?)。我不再遇到错误,会话似乎保持所有值。我的下一个问题是调试器现在将“跳过”视为“继续”。奇数。

标签: c# asp.net session nullreferenceexception


【解决方案1】:

看来我没有在我的SessionStore 类中初始化一些对象,因为我添加了一个初始化它们的构造函数,现在问题已经解决了。也许问题实际上是在对象被序列化时发生的(就像将对象存储到状态服务器中的情况一样),并且错误消息让我感到困惑。

编辑 - 我总是这样做...在我发布到 StackOverflow 之后找出解决方案... :(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2021-04-29
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多