dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Where is this context performing a second operation?

  1. #1
    Join Date
    Aug 2019
    Posts
    5

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,033

    Re: Where is this context performing a second operation?

    GetOwinContext is obsolete. What .net version are you using?

  3. #3
    Join Date
    Aug 2019
    Posts
    5

    Re: Where is this context performing a second operation?

    Quote Originally Posted by Arjay View Post
    GetOwinContext is obsolete. What .net version are you using?
    I believe 4.7.2

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,033

    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.

  5. #5
    Join Date
    Aug 2019
    Posts
    5

    Re: Where is this context performing a second operation?

    Quote Originally Posted by Arjay View Post
    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.

  6. #6
    Join Date
    Aug 2019
    Posts
    5

    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.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,033

    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.

  8. #8
    Join Date
    Aug 2019
    Posts
    5

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)