在上一个异步操作结束之前,我在上下文中收到有关第二个异步操作的错误,但是我看不到它的发生位置。这是我正在使用的代码。每个异步调用都在使用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”来确保在此上下文上调用另一个方法之前,所有异步操作都已完成。不保证任何实例成员都是线程安全的。
以下是通过网络服务调用显示的例外情况:
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.
答案 0 :(得分:2)
最有可能
where s.id == assessment.Lab_Space.id
在不适当的时间触发Lab_Space
导航属性的延迟加载。
假设您没有像Lab_Space_Id
这样的显式FK属性(如果确实有,只需使用它而不是Lab_Space.id
),要么急于加载它(最好):
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 // <--
...
请注意,在原始查询中,assessment.Lab_Space
访问只是在查询表达式树中记录。实际评估(因此会触发延迟加载)是查询执行的一部分。