【问题标题】:.net web forms application gives "Object reference not set to an instance of an object" for some users on chrome.net Web 表单应用程序为 chrome 上的某些用户提供“对象引用未设置为对象的实例”
【发布时间】:2015-02-11 14:51:36
【问题描述】:

我制作了一个 Web 应用程序,并在本地对其进行了测试。一切正常,因此它被部署到托管服务器。 几周后,一些用户开始遇到“对象引用未设置为对象实例”错误。 目前这只发生在 chrome 浏览器中(在其他浏览器上,应用程序正常启动),如果他们在 chrome 中使用隐身模式,应用程序也可以正常打开。

这是整个错误消息,不幸的是我没有得到错误所在的代码行,即使我使用了

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   INAWebAppTest.SiteMaster.Page_Load(Object sender, EventArgs e) +884
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

我不知道这是否是因为服务器上部署了较新版本的应用程序以及用户 cookie?

如果是这样,您能否告知我可以尝试纠正此问题的方法。我不能要求所有用户都清理他们的浏览器历史记录和cookies,它们太多了,如果以后再发生,也无法解决这个问题。

这是来自网站管理员的代码:

    public partial class SiteLogin : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.Cookies["userInfo"] != null)
            {
                Response.Cookies.Set(Request.Cookies["userInfo"]);
                Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
                Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(7);
                if (Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]) == null)
                {
                    Response.Redirect("~/Account/login.aspx");
                }
                if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("3"))
                        Response.Redirect("~/Form/Progress.aspx");                 
                if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("2"))
                        Response.Redirect("~/CForm/CHome.aspx");
                if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("1"))
                        Response.Redirect("~/AForm/AHome.aspx");



            }
        }
    }
}

这是来自 global.asax 页面的代码:

        protected void Session_Start(object sender, EventArgs e)
    {
 string sessionId;
        if (Session.SessionID!=null)
            sessionId = Session.SessionID;
        else
        {
            SmtpClient client = new SmtpClient();
            client.Port = xxx;
            client.Host = "xxx.xxx.xxx.xxx";
            client.EnableSsl = false;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("");

            MailMessage mm = new MailMessage();
            mm.From = new MailAddress("xxx", "xxx");
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mm.Subject = "Null object";
            mm.IsBodyHtml = true;
            mm.To.Add("xxx");
            string body = "null";
            mm.Body = body;
            try
            {
                client.Send(mm);

            }
            catch
            {

            }
}

在应用的第一个版本中,global.asax 是空的,但是在刷新会话时出现了一些问题,所以我搜索了一下,发现解决方案是这行代码:

     string sessionId = Session.SessionID;

这解决了这个问题,但由于一些用户遇到了本主题中提到的问题,我添加了一些代码,以便在找不到会话变量时收到一条消息。我从来没有收到过这封邮件,所以我认为问题不存在。

感谢您的帮助!

【问题讨论】:

  • 您是否正在使用和/或设置任何Session[] 变量..?如果你是..那么我会推荐两件事..1. 初始化Globala.asax 文件中的所有会话变量,2 使用HttpContext.Current.Session 而不是Session 如果这不是问题,请显示引发此错误的相关代码..如果您不确定..然后使用Debugger确定它发生的位置
  • 你能把抛出异常的代码贴出来吗,看看它在Page_LoadSiteMaster 中的堆栈跟踪。可能是您在请求中引用的某些内容可能不是从 chrome 设置的
  • 感谢您的回答!我编辑了我的问题并添加了来自 global.asax 和 page master 的代码。请您再看一遍好吗?不幸的是我没有这个问题(大多数用户没有报告这个问题),应用程序在 chrome 浏览器中为我正常启动,所以我无法调试它。

标签: c# asp.net google-chrome cookies


【解决方案1】:

我们确实没有足够的信息来准确地告诉您出了什么问题,从您所包含的跟踪中我只能看出异常是在您的母版页的 Page_Load 方法中引发的。

查看此方法并检查您在不检查对象是否存在的情况下访问对象属性的任何点。根据我的经验,常见的罪魁祸首是从会话或 cookie 集合中提取内容并尝试使用它们而不检查您是否只是拉出空值。解决此问题的最佳方法是您可以通过使用 chrome 并逐步执行 Page_Load 方法自己复制此问题。

编辑:在看到您发布的源代码后,我没有看到任何立即突出破坏 chrome 兼容性的内容,您有一些奇怪的 cookie 逻辑 imo,但没有任何破坏。我唯一的建议是让您尝试进一步复制这一点,并在您的解决方案中添加更多日志记录,以便当您的用户遇到此异常时,您可以获得更多信息。

我过去使用的一种技术是在调试模式下部署站点,并使用所有 .pdb 文件,并使用如下内容:

var stack = new StackTrace(exception, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();

然后记录该值。它应该为您提供引发异常的行号,尽管正如我所说,您的代码中没有任何内容对我来说是错误的,所以我不完全确定。

【讨论】:

  • 感谢您的回答!我编辑了我的问题并添加了来自 global.asax 和 page master 的代码。请您再看一遍好吗?不幸的是我没有这个问题,应用程序在 chrome 浏览器中为我正常启动,所以我无法调试它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多