我得到了关于在上一个异步操作结束之前在上下文上启动第二个异步操作的错误,但我没有看到它发生在哪里。这是我正在使用的代码。每个异步调用都使用await,那么我做错了什么?
这是Web API 2调用上的一种方法。
public async Task<IHttpActionResult> SendAssessmentArsAsync(int assessmentId) {
using (var context = new LAMPEntities()) {
var assessment = await context.EHS_Assessment_Audit.AsNoTracking().Where(x => x.id == assessmentId).FirstOrDefaultAsync();
var arsQuery = from r in context.EHS_Assessment_Audit_AR.AsNoTracking()
where r.EHS_Assessment_Audit_Id == assessmentId
join w in context.Worker on r.Assignee_WWID equals w.WWID
select new {
w.Email,
w.Full_Name,
r.AR,
r.Due_Date
};
var ars = await arsQuery.ToArrayAsync();
var lab = from s in context.Lab_Space.AsNoTracking()
where s.id == assessment.Lab_Space.id
join w in context.Worker.AsNoTracking() on s.Contact_WWID equals w.WWID
where w.Email != null
join d_join in context.Worker.AsNoTracking() on s.Delegate equals d_join.WWID into d_grp
from d in d_grp.DefaultIfEmpty()
select new {
Owner = w.Email,
Delegate = d.Email,
Barcode = s.Entry_Bar_Code,
Label = s.Floor_Space_Label,
Id = s.id
};
var mails = await lab.FirstAsync();
当它到达最后一行时,就是抛出异常的地方。
异常消息:
在上一个异步操作完成之前,在此上下文上启动了第二个操作。使用“await”确保在该上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不能保证线程安全。
以下是通过webservice调用显示的异常:
b__a()
at System.Data.Entity.Core.Objects.ObjectContext.d__3d`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.d__9`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
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.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
at System.Data.Entity.Internal.LazyAsyncEnumerator`1.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.d__1d`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at LabSORService.Controllers.EHSController.d__21.MoveNext() in ...\\Controllers\\EHSController.cs:line 854
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskHelpersExtensions.d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()" }
{
"message": "An error has occurred.",
"exceptionMessage": "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.",
"exceptionType": "System.NotSupportedException",
"stackTrace": " at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue)
at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression)
at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassc.
最有可能
where s.id == assessment.Lab_Space.id
正在不适当的时间触发延迟加载Lab_Space导航属性。
假设您没有显式的FK属性,如Lab_Space_Id
(如果您有,只需使用它而不是Lab_S
),要么急于加载它(最好):
var assessment = await context.EHS_Assessment_Audit.AsNoTracking()
.Include(x => x.Lab_Space) // <--
.Where(x => x.id == assessmentId).FirstOrDefaultAsync();
或在查询之外对其进行评估:
var labSpaceId = assessment.Lab_Space.id; // <--
var lab = from s in context.Lab_Space.AsNoTracking()
where s.id == labSpaceId // <--
...
请注意,在原始查询中,评估。Lab_Space
访问只是记录在查询表达式树中。实际评估(因此触发延迟加载)作为查询执行的一部分发生。
当我开始我的页面时,一切都加载正常,我第二次尝试重新加载,我收到以下错误,我从角度运行网址 在上一个异步操作完成之前,在此上下文上启动了第二个操作。使用“await”确保在该上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不能保证线程安全。 这就是方法 在这个类中,调用将成为数据库的上下文 这种情况不仅发生在这个方法中,当我尝试调用多个时也会发生
消息: 代码: 我尝试的: 我不知道我做错了什么,我尝试了不同的方法,比如: 1. 2. 3. 我做错了什么?
我有一个循环,在循环中我正在做: 第二种方法使用EF获取实体,设置一些属性,并通过调用DbContext上的wait SaveChangesAsync()将实体保存回数据存储。 上面应该等待1s,然后继续第二种方法。通过上述实现,我得到以下异常: 在上一个异步操作完成之前,在此上下文上启动了第二个操作。使用“await”确保在该上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不能保证线
在我的控制器中,我在保存到DB的日期之后执行另一个Void函数。但有时会出现这种错误: 在上一个异步操作完成之前,在此上下文上启动了第二个操作。使用“await”确保在该上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不能保证线程安全。 ...下面是我的控制器示例: 下面是Void方法: 我怎么解决这个问题?
在winform应用程序中使用EntityFrmework 6.2异步方法时,我遇到了一个问题。我们使用自定义类来管理基于单个活动设置的同步或异步EntityFrmework访问。当我们使用同步访问时,一切正常。当我们使用异步调用时,我们遇到了这个问题:在上一个异步操作完成之前,在此上下文上启动了第二个操作。使用“await”确保在该上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不能
概述 定时器 Promise 对象