3

時折 (約 1 か月に 1 回)、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())

この例外が一度発生した瞬間から、アプリケーションは使用できなくなります。すべてのビューがこのエラーをスローし始めます。ソースコードを調べてみましたが、何も見つからないようです。同じ動作を経験していても、エラーコードが異なる人がいます。

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

誰かがアイデアを持っている場合は、自由に共有してください。

4

1 に答える 1

0

運用チームに問い合わせたところ、時間間隔が経過した後、アプリケーションプールがリサイクルされていたようです。これは、アプリケーションに大きな負荷がかかっている営業時間中に発生しました。特定の時間にリサイクルするように変更しました。これはまだ回避策ですが、問題が頻繁に発生するのを防ぐ必要があります。

于 2013-01-29T09:43:52.883 に答える