简体   繁体   English

为什么在已经使用await时要求我使用它(在此上下文中启动了第二个操作)?

[英]Why am I asked to use await when I am already doing so (A second operation started on this context)?

I have what seems like a relatively simple HTTP GET request to acquire the number of user notifications a specific user has. 我有一个似乎相对简单的HTTP GET请求,用于获取特定用户具有的用户通知数量。 The problem occurs about 50% of the time. 大约50%的时间出现此问题。 The request is done through an AJAX call on the client every 10 seconds or so. 该请求是每10秒钟左右通过客户端上的AJAX调用完成的。 Similar problems (though not as frequent) have occurred in other areas of the application when UserManager is performing awaitable calls. 当UserManager执行等待的调用时,在应用程序的其他区域中也发生了类似的问题(尽管不那么频繁)。

I have tried to be sure that other async operations are not occurring by gutting the code in this path to the bare essentials. 我试图通过将这条基本内容的路径压缩掉来确保不会发生其他异步操作。

[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;
    }
}

I often get this exception when running, but only sometimes (I have cut the output down to the offending line): 在运行时,我经常会遇到此异常,但仅在某些情况下(我已将输出削减到令人讨厌的行):

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()"}

This line is shown above: var currentUser = await um.FindByNameAsync(username); 该行如上所示: var currentUser = await um.FindByNameAsync(username);

EDIT: I changed the original notification code presented in the question because it was confusing users. 编辑:我更改了问题中提出的原始通知代码,因为它使用户感到困惑。 The code above is now the entire class involved in the problem. 上面的代码现在是问题中涉及的整个类。 It focuses on the simple task of returning the user id. 它着重于返回用户ID的简单任务。 It is not a real part of my application (though this is what I am running), but the problem occurring simplifies to this example. 它不是我的应用程序的真实部分(尽管这是我正在运行的内容),但是出现的问题简化了此示例。 Let me know if there are other sections of the code that may be helpful to post. 让我知道代码中是否还有其他部分可能对发布有所帮助。

You're making simultaneous calls to the same DbContext . 您正在同时调用同一DbContext The stack trace says so. 堆栈跟踪就是这样。 So would the type of the exception if you had posted it. 如果您发布了异常,则其类型也会如此。

My problem was in my CookieAuthenticationOptions Provider . 我的问题出在我的CookieAuthenticationOptions Provider In a custom OnValidateIdentity , I had used an example that I found to store a claim for the users expiration time. 在自定义的OnValidateIdentity ,我使用了一个示例,发现该示例存储了用户到期时间的声明。 This example was invoking the SecurityStampValidator without awaiting. 这个例子是在不等待的情况下调用SecurityStampValidator This is what was causing intermittent problems. 这就是造成间歇性问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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