-
August 12th, 2019, 09:06 AM
#1
Where is this context performing a second operation?
I have what seems like a relatively simple HTTP API GET request to acquire the number of user notifications a specific user has. The problem occurs about 50% of the time. The request is done through an AJAX call on the client every 10 seconds or so. Similar problems (though not as frequent) have occurred in other areas of the application when UserManager is performing awaitable calls.
Code:
[Authorize]
public class NotificationsApiController : ApiController
{
[Route("api/NotificationsApi/GetNotificationsCount/")]
public async System.Threading.Tasks.Task<string> GetUserIdAsync()
{
string userId = null;
string username = HttpContext.Current.User.Identity.Name;
ApplicationUserManager um = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = await um.FindByNameAsync(username);
userId = currentUser.Id;
return userId;
}
}
When the error occurs, I get this exception stack:
Code:
System.NotSupportedException
A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
at System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered()
at System.Data.Entity.Core.Objects.ObjectQuery`1.System.Data.Entity.Infrastructure.IDbAsyncEnumerable<T>.GetAsyncEnumerator()
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<FirstOrDefaultAsync>d__25`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 Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<GetUserAggregateAsync>d__67.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 CORT.Controllers.NotificationsApiController.<GetUserIdAsync>d__0.MoveNext() in Controllers\WebAPIs\NotificationsApiController.cs:line 24
--- 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.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`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.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>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.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.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.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.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.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"}
I have tried digging around quite a bit trying to discover where the non awaited call may be. It is my understanding that this is a typical way to get the user id. If there is any other code that I can share that may help, please let me know. Thanks.
-
August 12th, 2019, 05:29 PM
#2
Re: Where is this context performing a second operation?
GetOwinContext is obsolete. What .net version are you using?
-
August 12th, 2019, 06:16 PM
#3
Re: Where is this context performing a second operation?
Originally Posted by Arjay
GetOwinContext is obsolete. What .net version are you using?
I believe 4.7.2
-
August 12th, 2019, 06:35 PM
#4
Re: Where is this context performing a second operation?
Have you looked into using the newer alternatives? I ran into several when searching for GetOwinContext.
-
August 12th, 2019, 06:47 PM
#5
Re: Where is this context performing a second operation?
Originally Posted by Arjay
Have you looked into using the newer alternatives? I ran into several when searching for GetOwinContext.
No, I didn't even know that was considered deprecated. I've posted this issue in other places and no one has mentioned that.
-
August 12th, 2019, 08:59 PM
#6
Re: Where is this context performing a second operation?
Are you sure it's deprecated? I'm not finding much info about that. I did find that with mvc 5, forms authentication is deprecated in favor of owin.
-
August 13th, 2019, 12:25 PM
#7
Re: Where is this context performing a second operation?
I saw it deprecated in a couple of threads I read. At any rate, could the following be your problem? https://santoshpoojari.blogspot.com/...eturn.html?m=1
With so many .net versions out there, it's hard to track down. If you able to move your project to the latest .net, it might help you get it resolved. Not very good advice, sorry.
-
August 13th, 2019, 12:30 PM
#8
Re: Where is this context performing a second operation?
It's all good. I appreciate the response. We are ahead of the version mentioned, and I'm not getting null issues.
Could you point me to the alternatives you were talking about? I would work around the getting the user ID code, but I also use the usermanager to obtain user roles.
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|