![](/img/trans.png)
[英]Why am I asked to use await when I am already doing so (A second operation started on this context)?
[英]Why Does Await Not Appear to Prevent Second Operation on EF Context
在ASP.NET MVC应用程序中,对于使用我的实体框架上下文的一种控制器方法,我收到以下错误消息。
在先前的异步操作完成之前,第二操作在此上下文上开始。 使用“ await”来确保在此上下文上调用另一个方法之前,所有异步操作都已完成。 不保证任何实例成员都是线程安全的。
我知道您不能并行运行查询,并且一切似乎都已正确等待。 如果我调试程序并逐步检查从EF返回的一些数据,则它可以工作,可能是因为这迫使查询完成。
编辑如果我把一个断点在控制器方法空校验和检查的数据shipmentDetail
的没有抛出异常。
这是代码片段:
控制器方式 :
[Route("{id:int}/Deliveries")]
public async Task<ActionResult> DeliveryInfo(int id)
{
var shipmentDetail = await db.ShipmentDetails.SingleOrDefaultAsync(s => s.Id == id);
if (shipmentDetail == null)
return HttpNotFound(string.Format("No shipment detail found with id {0}", id));
var model = await DeliveryInfoModel.CreateModel(db, shipmentDetail);
return View("DeliveryInfo", model);
}
CreateModel方法 :
public static async Task<DeliveryInfoModel> CreateModel(Context db, ShipmentDetail shipment)
{
DeliveryInfoModel model = new DeliveryInfoModel()
{
ShipmentInfo = shipment
};
//initialize processing dictionary
Dictionary<int, bool> boxesProcessed = new Dictionary<int, bool>();
List<DeliveryBoxStatus> statuses = new List<DeliveryBoxStatus>();
for (int i = 1; i <= shipment.BoxCount; i++ )
{
boxesProcessed.Add(i, false);
}
//work backwards through process
//check for dispositions from this shipment
if(shipment.Dispositions.Count > 0)
{
foreach (var d in shipment.Dispositions)
{
DeliveryBoxStatus status = new DeliveryBoxStatus()
{
BoxNumber = d.BoxNumber,
LastUpdated = d.Date,
Status = d.Type.GetDescription().ToUpper()
};
statuses.Add(status);
boxesProcessed[d.BoxNumber] = true;
}
}
//return if all boxes have been accounted for
if (boxesProcessed.Count(kv => kv.Value) == shipment.BoxCount)
{
model.BoxStatuses = statuses;
return model;
}
//check for deliveries
if(shipment.Job_Detail.Count > 0)
{
foreach (var j in shipment.Job_Detail.SelectMany(d => d.DeliveryInfos))
{
DeliveryBoxStatus status = new DeliveryBoxStatus()
{
BoxNumber = j.BoxNumber,
LastUpdated = j.Job_Detail.To_Client.GetValueOrDefault(),
Status = "DELIVERED"
};
statuses.Add(status);
boxesProcessed[j.BoxNumber] = true;
}
}
//check for items still in processing & where
foreach (int boxNum in boxesProcessed.Where(kv => !kv.Value).Select(kv => kv.Key))
{
//THIS LINE THROWS THE EXCEPTION
var processInfo = await db.Processes.Where(p => p.Jobs__.Equals(shipment.Job.Job__, StringComparison.InvariantCultureIgnoreCase) && p.Shipment == shipment.ShipmentNum && p.Box == boxNum)
.OrderByDescending(p => p.date)
.FirstOrDefaultAsync();
//process returned data
//...
}
model.BoxStatuses = statuses;
return model;
}
我不确定是由于控制器中的查询还是由于循环中的查询未完成导致此行为。 由于EF的懒惰,何时真正执行/返回查询,或者在这种情况下异步/等待如何工作,我是否不了解? 我还有很多其他方法和控制器可以进行异步EF调用,并且以前没有遇到过这种情况。
编辑
我的上下文是使用Ninject作为我的IoC容器注入到我的控制器中的。 这是NinjectWebCommon的RegisterServices方法内部的配置:
kernel.Bind<Context>().ToSelf().InRequestScope();
与Entity Framework异步使用时,请避免延迟加载。 取而代之的是,要么先加载您需要的数据,要么使用Include()
来确保查询中加载了您需要的数据。
https://msdn.microsoft.com/zh-CN/magazine/dn802603.aspx
异步支持的当前状态
...异步支持已在版本6中添加到了Entity Framework(在EntityFramework NuGet包中)。 但是,由于异步加载始终是同步执行的,因此必须小心避免延迟加载 。 ...
(强调我的)
也:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.