我在 stack overflow 上发了这个问题,但无济于事。现在很绝望。
因此,在我们的生产环境中,我们的 C# ASP.NET MVC5 应用程序中出现了一个极其随机的错误(我的意思是,在错误之前似乎没有使用模式)。
该错误似乎仅在用户登录后偶尔发生,并且整个系统崩溃并且所有用户都无法访问,直到我们手动进入 IIS8 并重新启动 Web 服务器。我们在带有 IIS 的 Windows Server 2012 环境中运行 SQL Server Express 2012 SP2。
错误信息如下:
IndexOutOfRangeException/Account/Login
Index was outside the bounds of the array.
堆栈跟踪如下:
IndexOutOfRangeException·Index was outside the bounds of the array.
Raw
:0System.Data.SqlClient.SqlDataReader.CheckHeaderIsReady(Int32 columnIndex, Boolean permitAsync, String methodName)
:0System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
:0System.Data.Entity.Core.Common.Internal.Materialization.Shaper+ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
:0lambda_method(Closure , Shaper )
:0System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
:0lambda_method(Closure , Shaper )
:0System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
:0System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1+SimpleEnumerator+<MoveNextAsync>d__4.MoveNext()
:0System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
:0System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
:0System.Data.Entity.Internal.LazyAsyncEnumerator`1+<FirstMoveNextAsync>d__0.MoveNext()
:0System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
:0System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
:0System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions+<FirstOrDefaultAsync>d__25`1.MoveNext()
:0System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
:0System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
:0Microsoft.AspNet.Identity.TaskExtensions+CultureAwaiter`1.GetResult()
:0Microsoft.AspNet.Identity.EntityFramework.UserStore`6+<GetUserAggregateAsync>d__6c.MoveNext()
:0System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
:0System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
:0Microsoft.AspNet.Identity.TaskExtensions+CultureAwaiter`1.GetResult()
:0Microsoft.AspNet.Identity.UserManager`2+<FindAsync>d__12.MoveNext()
:0System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
:0System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
:0Saleboat.Controllers.AccountController+<Login>d__9.MoveNext()
:0System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
:0System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
:0System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)
:0System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult)
:0System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
:0System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
:0System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters+<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
:0System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters+<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
:0System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
:0System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21+<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
:0System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
此错误仅发生在我们的实时服务器/数据库中,无法故意重现,我们几乎必须等待它发生并检查 Bugsnag 捕获的异常。
编辑
以下是登录时执行的代码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
var userId = UserManager.FindByName(model.UserName).Id;
int companyId = db.Users.Find(userId).CompanyId;
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
return View(model);
}