【问题标题】:Linq to SQL NullReferenceException's: A random needle in a haystack!Linq to SQL NullReferenceException's:大海捞针!
【发布时间】:2011-02-07 16:25:51
【问题描述】:

我在我的应用程序中看似随机地收到 NullReferenceExeceptions,但无法追踪可能导致错误的原因。

我会尽力描述场景和设置。

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

  • C# .net 3.5 表单应用程序,但我使用 Phil Haack (http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx) 构建的 WebFormRouting 库来利用 .net 的路由库(通常与 MVC 结合使用) - 而不是使用 url 重写我的网址。

  • 我的数据库有 60 个表。全部归一化。这只是一个庞大的应用程序。 (SQL 服务器 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 上的“自动关闭”标志设置。这被设置为真,我改为假。虽然整体效果不错,但似乎没有成功。

这是未捕获的详细信息。当我的 try/catch's 捕获时,我也会得到稍微不同的错误。

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)

求助!!!

【问题讨论】:

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

标签: linq-to-sql nullreferenceexception


【解决方案1】:

从堆栈跟踪来看,它看起来与 LINQ 没有任何关系。在 Reflector 中查看 System.Web 会发现 Page.RequestViewStateString 不知何故为空。当异常发生时,我会在调试器中确认这一点,并尝试从那里追溯发生的事情(使用反射器)。

【讨论】:

  • 感谢 Evgeny,现在看看。
【解决方案2】:

天哪,十指相扣。

遇到了这个。 http://forums.asp.net/t/1170677.aspx

它谈到设置的地方:页面中的 enableEventValidation="false" 或所有页面的 web.config 中。

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

在直播网站上,我现在很乐意使用创可贴。

感谢 Evgeny,你让我走上了正轨。我想我错误地怀疑 Linq to SQL,从堆栈跟踪中可能更明显。

不幸的是(或幸运地),我正在尝试很多“新东西”,所以很多尚未发现的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2010-11-17
    • 2019-05-19
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多