NullReferenceException in WebConfigScopeDictionary

▼魔方 西西 提交于 2020-01-11 10:01:55

问题


Occasionally (about once a month) an ASP.NET MVC 3 web application fails with this exception.

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)

This exception occurs on a line with

@using (Html.BeginForm())

From the moment this exception occurs once, the application is unusable. Every view will start throwing this error. I've looked around in the source code but seem unable to find anything. There are people who experience the same behaviour but with a different error and or by different code.

When digging deeper in the source of WebConfigScopeDictionary you can see that the AppSettings are being read:

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));
}

The appsettings come from the 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;
    }
}

If there would be a problem with the web.config this code should have thrown an exception. But it doesn't. Eventually count is called on the collection that is returned by the AppSettings property. Since it is probably null at this point it throws.

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;
}

If anyone has any ideas, feel free to share.


回答1:


After enquiring with the ops team it would appear that the application pool was being recycled after the timed interval had expired. This happened during office hours while the application is under a significant load. We've now changed it to recycle at a specific time, which is still a workaround but should prevent the problem from occurring too frequently.



来源:https://stackoverflow.com/questions/14577735/nullreferenceexception-in-webconfigscopedictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!