Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我已经在使用wait(在这个上下文中启动了第二个操作)?_C#_Asp.net Mvc_Exception_Async Await - Fatal编程技术网

C# 为什么我已经在使用wait(在这个上下文中启动了第二个操作)?

C# 为什么我已经在使用wait(在这个上下文中启动了第二个操作)?,c#,asp.net-mvc,exception,async-await,C#,Asp.net Mvc,Exception,Async Await,我有一个相对简单的HTTP GET请求来获取特定用户的用户通知数量。大约50%的情况下都会出现问题。该请求通过每10秒左右对客户端进行一次AJAX调用来完成。当UserManager执行等待调用时,在应用程序的其他区域也发生了类似的问题(尽管不那么频繁) 我已经尝试通过删除此路径中的代码来确保其他异步操作不会发生 [Authorize] public class NotificationsApiController : ApiController { [Route("api/Notifi

我有一个相对简单的HTTP GET请求来获取特定用户的用户通知数量。大约50%的情况下都会出现问题。该请求通过每10秒左右对客户端进行一次AJAX调用来完成。当UserManager执行等待调用时,在应用程序的其他区域也发生了类似的问题(尽管不那么频繁)

我已经尝试通过删除此路径中的代码来确保其他异步操作不会发生

[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;
    }
}
[授权]
公共类通知SAPIController:ApiController
{
[路由(“api/NotificationsApi/GetNotificationScont/”)
public async System.Threading.Tasks.Task GetUserIdAsync()
{
字符串userId=null;
字符串username=HttpContext.Current.User.Identity.Name;
ApplicationUserManager um=HttpContext.Current.GetOwinContext().GetUserManager();
var currentUser=await um.FindByNameAsync(用户名);
userId=currentUser.Id;
返回用户标识;
}
}
我在运行时经常会遇到这种异常,但只有在某些时候(我将输出减少到令人不快的行):

System.NotSupportedException
在上一个异步操作完成之前,在此上下文上启动了第二个操作。使用“await”确保在此上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不能保证线程安全。
位于System.Data.Entity.Internal.ThrowingMonitor.EnsureRenoted()处
位于System.Data.Entity.Core.Objects.ObjectQuery`1.System.Data.Entity.Infrastructure.IDBSyncEnumerable.GetAsyncEnumerator()处
位于System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.d_u25`1.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
位于Microsoft.AspNet.Identity.TaskExtensions.CultureAwater`1.GetResult()
在Microsoft.AspNet.Identity.EntityFramework.UserStore`6.d__67.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在Controllers\webapi\NotificationsApiController.cs中的CORT.Controllers.NotificationsApiController.d_u0.MoveNext()处:第24行
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Threading.Tasks.TaskHelperExtensions.d_u1`1.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Web.Http.Controllers.ApiControllerActionInvoker.d_u1.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Web.Http.Controllers.ActionFilterResult.d_u5.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Web.Http.Filters.AuthorizationFilterAttribute.d_u3.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Web.Http.Dispatcher.HttpControllerDispatcher.d_u15.MoveNext()“}
这一行如上图所示:
var currentUser=wait um.FindByNameAsync(用户名);


编辑:我更改了问题中显示的原始通知代码,因为它让用户感到困惑。上面的代码现在是问题涉及的整个类。它关注于返回用户id的简单任务。它不是我应用程序的真实部分(尽管这是我正在运行的),但出现的问题简化为本例。请告诉我是否有其他代码部分可能有助于发布。

您同时调用同一
DbContext
。堆栈跟踪显示是这样的。如果您发布了异常,异常的类型也会如此。

您同时调用同一de>DbContext。堆栈跟踪是这样说的。如果您发布了异常,异常的类型也会如此。

我的问题是我的
CookieAuthenticationOptions
提供程序中的问题。在自定义
OnValidateIdentity
中,我使用了一个我发现的示例来存储用户过期时间的声明。这个示例正在调用
SecurityStampValidator
没有等待。这就是导致间歇性问题的原因。

我的问题在于我的
CookieAuthenticationOptions
提供程序。在自定义的
OnValidateIdentity
中,我使用了一个我发现的示例来存储用户过期时间的声明。这个示例是inv在不等待的情况下关闭
SecurityStampValidator
。这就是导致间歇性问题的原因。

看起来与
UserManager
和/或
rolemanger
的并发性不是预期的,或者
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()"}