UserManager错误 – 在上一个异步操作完成之前,在此上下文中启动了第二个操作

我使用Identity v2.0.0.0,EF 6,Castle Windsor IOC Container,Microsoft SQL Server 2005,使用我的asp.net MVC5 Web应用程序来解决这个问题

我试图通过使用UserManagerExtensions,FindById()方法获取当前登录用户,但它抛出一个错误“System.NotSupportedException:在上一个异步操作完成之前在此上下文上启动了第二个操作。使用’await’确保在调用此上下文中的另一个方法之前已完成任何异步操作。不保证所有实例成员都是线程安全的“

我从堆栈跟踪中理解的是“AsyncHelper.RunSync()”导致问题,我没有在我的代码中使用任何异步函数,而是使用AsyncHelper参考的“FindById()” – : http://www.symbolsource。组织/ MyGet /元/ aspnetwebstacknightly /项目/ Microsoft.AspNet.Identity.Core / 2.0.0-beta1-140129 /发行/默认/ Microsoft.AspNet.Identity.Core / Microsoft.AspNet.Identity.Core /扩展/ UserManagerExtensions。 CS?ImageName = Microsoft.AspNet.Identity.Core

关于此Identity错误的信息似乎很少,我在应用程序的不同部分使用当前登录用户的详细信息,当我的服务层调用getCurrentLoggedInUser()函数时,这个错误似乎是随机发生的。 请帮忙….

我的代码和堆栈跟踪—-注意:我发现了另一个类似类型的事件: https : //aspnetidentity.codeplex.com/workitem/2408

Global.asax中:

.Register(Component .For<UserManager>() .UsingFactoryMethod((kernel, creationContext) => new TempoUserManager(new UserStore(new ApplicationDbContext(ASPUserDatabaseConnectionString)))) .LifestylePerWebRequest() ) .Register(Component .For<RoleManager>() .UsingFactoryMethod((kernel, creationContext) => new RoleManager(new RoleStore(new ApplicationDbContext(ASPUserDatabaseConnectionString)))) .LifestylePerWebRequest() ) 

服务层代码:

 public class GenericService { protected IRepository _repository; protected UserManager _userManager; public GenericService(IRepository repository, UserManager userManager) { _repository = repository; _userManager = userManager; } public ApplicationUser GetCurrentLoggedInUser() { //Error is thrown when calling this return _userManager.FindById(HttpContext.Current.User.Identity.GetUserId()); } } 

下面是堆栈跟踪,

 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.Enter() at System.Data.Entity.Core.Objects.ObjectQuery`1.d__e.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.Data.Entity.Internal.LazyAsyncEnumerator`1.d__0.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.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.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.AsyncHelper.RunSync[TResult](Func`1 func) at Microsoft.AspNet.Identity.UserManagerExtensions.FindById[TUser,TKey](UserManager`2 manager, TKey userId) at Tempo.BusinessLogics.Services.GenericService.GetCurrentLoggedInUser() at Tempo.BusinessLogics.Services.GenericService.GetCurrentLoggedInUserID() at Tempo.BusinessLogics.Services.TimeService.UpdateSignOfChildDetails(EntryLog toUpdate, Boolean targetSignState) at Tempo.BusinessLogics.Services.TimeService.SignEntries(SignOffs sgnOffs, Byte flag) at Tempo.Controllers.TimeEntryController.SignOffAll(String cbxStatus)** at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.c__DisplayClass1.b__0(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.c__DisplayClass48.b__41() at System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass33.b__32(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass21.c__DisplayClass2b.b__1c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass21.b__1e(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) at System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) at System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

也许你忘了await异步函数,然后下一个语句试图在单个连接上执行两个查询?

听起来你正在创建两个上下文,这两个上下文都是相同类型的ApplicationDbContext 。 当EF检测到同时发生的两个呼叫到同一个上下文时,您会收到错误消息。

您将要么必须为每个Web请求仅创建一个上下文实例,要么将上下文拆分为它的组件部分,以便可以注入两者而不会发生冲突。