首页 > 解决方案 > Microsoft.AspNetCore.Routing.RouteValueDictionary 的 System.IndexOutOfRangeException

问题描述

我们在 Azure 上生产了一个 .NET Core 2.1 应用程序。它很少出现无法恢复的 500 错误,需要重新启动站点。我们注意到,在这些时间,我们的 Azure 应用程序洞察日志中出现了数百次以下异常/堆栈跟踪:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Microsoft.AspNetCore.Routing.RouteValueDictionary.ListStorage.Clear()
   at Microsoft.AspNetCore.Routing.RouteValueDictionary.Clear()
   at Microsoft.AspNetCore.Routing.RouteData.RouteDataSnapshot.Restore()
   at Microsoft.AspNetCore.Routing.RouteCollection.<RouteAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.<Invoke>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext()

它令人困惑和令人沮丧,因为乍一看,所涉及的代码似乎都不是我们的,但也许是由于我们的路由中的某种错误配置?

作为参考,我们的 UseMvc 调用:

app.UseMvc(routes =>
{
    var section = Configuration.GetSection("AppSettings:HostNames");
    var hostnames = section.Get<string[]>();

    // Prepare special routes
    routes.MapRoute("takeSurveyApi", "TakeSurvey/{action}/{id?}", new { controller = "TakeSurvey", action = "Index" });
    routes.MapRoute("takeSurvey", "TakeSurvey/{id?}", new { controller = "TakeSurvey", action = "Index" });
    routes.MapRoute("stripeWebhook", "Billing/WebHook", new { controller = "Billing", action = "WebHook" });

    routes.MapSubdomainRoute(
        hostnames,
        "MHP",
        "{tenant}",
        "MHP/{action}",
        new { controller = "MHP", action = "GetTenantDetails" });

    routes.MapSubdomainRoute(
        hostnames,
        "Localisation",
        "{tenant}",
        "Localisation/{action}",
        new { controller = "Localisation", action = "Get" });

    routes.MapSubdomainRoute(
        hostnames,
        "TenantInSubdomain",
        "{tenant}",
        "{*url}",
        new { controller = "Home", action = "Index" });

    routes.MapRoute(
        hostnames,
        "DefaultRoute",
        "{controller}/{action}/{id?}",
        new { controller = "Home", action = "Index" });               

    routes.MapRoute(
        hostnames, "reactRouter", "{*url}", new {controller = "Home", action = "Index"});
});

有没有人遇到过类似的情况?

标签: c#asp.net-core

解决方案


推荐阅读