2010-04-13 55 views
1

我在我的应用程序的随机时间看到NullReferenceExeceptions,无法追查可能导致错误的原因。Linq to SQL NullReferenceException:干草堆中的随机针!

我会尽我所能来描述场景和设置。

任何和所有的建议非常感谢!

  • C#.NET 3.5窗体应用程序,但我用菲尔哈克(http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx)建成WebFormRouting库利用.NET(通常与MVC一起使用)的路由库 - 这一翻译使用URL重写的我的网站。

  • 我的数据库有60个表格。全部标准化。这只是一个巨大的应用程序。 (SQL Server 2008)

  • 所有查询都是使用Linq to SQL在代码(无SP)中构建的。每次创建我的数据上下文的新实例。我只使用一个数据上下文,并在SQL Server的4个关系图中定义了所有关系。

  • 数据上下文被创建了很多。我让数据上下文的结束自动处理。我已经听到了双方关于你是否应该自动关闭或者自己去关闭的争论。在这种情况下,我不要自己做。

  • 这似乎并不重要,如果我创建了很多数据上下文的实例或只是一个。

例如,我有一个按钮。用下面的代码,它的错误大概是10-20次之一。

protected void VoteUpLinkButton_Click(object sender, EventArgs e) 
{ 
    DatabaseDataContext db = new DatabaseDataContext(); 

    StoryVote storyVote = new StoryVote(); 
    storyVote.StoryId = storyId; 
    storyVote.UserId = Utility.GetUserId(Context); 
    storyVote.IPAddress = Utility.GetUserIPAddress(); 
    storyVote.CreatedDate = DateTime.Now; 
    storyVote.IsDeleted = false; 

    db.StoryVotes.InsertOnSubmit(storyVote); 
    db.SubmitChanges(); 

    // If this story is not yet published, check to see if we should publish it. Make sure that 
    // it is already approved. 
    if (story.PublishedDate == null && story.ApprovedDate != null) 
    { 
     Utility.MakeUpcommingNewsPopular(storyId); 
    } 

    // Refresh our page. 
    Response.Redirect("/news/" + category.UniqueName + "/" 
     + RouteData.Values["year"].ToString() + "/" 
     + RouteData.Values["month"].ToString() + "/" 
     + RouteData.Values["day"].ToString() + "/" 
     + RouteData.Values["uniquename"].ToString()); 
} 

我试过的最后一件事是SQL Server上的“自动关闭”标志设置。这被设置为true,我改为false。虽然整体效果很好,但似乎没有做到这一点。

这里有一个没有被捕获的详细信息。当我的try/catch被抓到时,我也会得到几乎不同的错误。

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> 
System.NullReferenceException: Object reference not set to an instance of an object. at 
System.Web.Util.StringUtil.GetStringHashCode(String s) at 
System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded() at 
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) at 
System.Web.UI.WebControls.TextBox.LoadPostData(String postDataKey, NameValueCollection postCollection) at 
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) at 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at 
System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at 
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at 
System.Web.UI.Page.ProcessRequest(HttpContext context) at 
ASP.forms_news_detail_aspx.ProcessRequest(HttpContext context) at 
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

HELP!

+0

顺便说一下,我在页面上使用了一些Telerik控件。如果有人认为这可能会导致错误。在仪表板上,我使用RadDock,它比我想要的要慢,但我不知道它是罪魁祸首。 – Shane 2010-04-13 22:34:51

回答

0

从堆栈跟踪看,它看起来不像LINQ的任何事情。在Reflector中查看System.Web,看起来Page.RequestViewStateString以某种方式为空。我会在调试器中确认发生异常时,并尝试追踪从那里发生的事情(使用反射器)。

+0

谢谢Evgeny,现在看看。 – Shane 2010-04-13 23:28:01

0

哦,男孩,手指交叉。

来过这个。 http://forums.asp.net/t/1170677.aspx

它在页面或web.config中为所有页面设置:enableEventValidation =“false”。

我宁愿不必这样做,在技术上它不能“修复”错误,只是通过外观避免它。

在生活网站上,我现在很乐意带上一个创可贴。

谢谢Evgeny,你让我走上正轨。我想我错误地怀疑Linq到SQL,它应该可能从栈跟踪更明显。

不幸的是(或者幸运的是),我正在尝试很多'新东西',所以很多还没有看到的错误。