简体   繁体   中英

ASP.net Web API's ActionFilterAttribute throws InvalidOperationException: Sequence contains more than one element

In our production environment some users cannot login through our Web API because the following exception is thrown on the server side:

System.InvalidOperationException: Sequence contains more than one element
   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext()

I cannot figure out how to reproduce this on the development environment. The error message is also very unclear and there's nothing from our code in the stack trace.

Have any of you experienced this issue? What could possibly cause it?

Spent half a day on this, found the cause, In the owin appbuilder chain app.UseOAuthBearerTokens(OAuthOptions); was called twice. This results in two similair AuthenticationResults resulting in a Sequence contains more than one element.

This error occurs when the collection doesn't contain exactly 0 or 1 elements and you are using .SingleOrDefault() .

Use .FirstOrDefault() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM