【问题标题】:NullReferenceException in WebConfigScopeDictionaryWebConfigScopeDictionary 中的 NullReferenceException
【发布时间】:2013-01-29 07:27:37
【问题描述】:

偶尔(大约每月一次)ASP.NET MVC 3 Web 应用程序会因此异常而失败。

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

Server stack trace: 
   at System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllKeys()
   at System.Collections.Specialized.NameValueCollection.get_AllKeys()
   at System.Web.WebPages.Scope.WebConfigScopeDictionary.<>c__DisplayClass4.<.ctor>b__0()
   at System.Lazy`1.CreateValue()

Exception rethrown at [0]: 
   at System.Lazy`1.get_Value()
   at System.Web.WebPages.Scope.WebConfigScopeDictionary.TryGetValue(Object key, Object& value)
   at System.Web.Mvc.ViewContext.ScopeGet[TValue](IDictionary`2 scope, String name, TValue defaultValue)
   at System.Web.Mvc.ViewContext.ScopeCache..ctor(IDictionary`2 scope)
   at System.Web.Mvc.ViewContext.ScopeCache.Get(IDictionary`2 scope, HttpContextBase httpContext)
   at System.Web.Mvc.ViewContext.GetClientValidationEnabled(IDictionary`2 scope, HttpContextBase httpContext)
   at System.Web.Mvc.Html.FormExtensions.FormHelper(HtmlHelper htmlHelper, String formAction, FormMethod method, IDictionary`2 htmlAttributes)
   at ASP._Page_Views_mywebpage_Create_cshtml.Execute() in c:\App\Views\Mywebpage\Create.cshtml:line 11
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

此异常发生在带有

的行上
@using (Html.BeginForm())

从该异常发生一次的那一刻起,应用程序就无法使用。每个视图都会开始抛出这个错误。我查看了源代码,但似乎找不到任何东西。有些人经历相同的行为,但有different erroror by different code

当深入挖掘 WebConfigScopeDictionary 的源代码时,您可以看到 AppSettings 正在被读取:

public WebConfigScopeDictionary() : this(WebConfigurationManager.AppSettings)
{
}
public WebConfigScopeDictionary(NameValueCollection appSettings)
{
    this._items = new Lazy<Dictionary<object, object>>(() => appSettings.AllKeys.ToDictionary((string key) => key, (string key) => appSettings[key], ScopeStorageComparer.Instance));
}

appsettings 来自 ConfigurationManager:

public static NameValueCollection AppSettings
{
    get
    {
        object section = ConfigurationManager.GetSection("appSettings");
        if (section == null || !(section is NameValueCollection))
        {
            throw new ConfigurationErrorsException(SR.GetString("Config_appsettings_declaration_invalid"));
        }
        return (NameValueCollection)section;
    }
}

如果 web.config 有问题,此代码应该引发异常。但事实并非如此。最终在 AppSettings 属性返回的集合上调用 count。由于此时它可能为 null,因此它会抛出。

protected string[] BaseGetAllKeys()
{
    int count = this._entriesArray.Count;
    string[] array = new string[count];
    for (int i = 0; i < count; i++)
    {
        array[i] = this.BaseGetKey(i);
    }
    return array;
}

如果有人有任何想法,请随时分享。

【问题讨论】:

  • 在挖掘 NameObjectCollectionBase 类时,据我所知,唯一可能存在的问题是反序列化过程中出现问题。但这将是一个框架错误......

标签: asp.net-mvc asp.net-webpages


【解决方案1】:

在向运营团队询问后,应用程序池似乎在时间间隔到期后被回收。这发生在办公时间,而应用程序处于大量负载之下。我们现在已将其更改为在特定时间回收,这仍然是一种解决方法,但应该可以防止问题过于频繁地发生。

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 2011-07-24
    • 2013-03-12
    • 2016-09-20
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    相关资源
    最近更新 更多