EF6 ToListAsync不运行异步但阻止线程 [英] EF6 ToListAsync does not run async but blocks the thread

查看:607
本文介绍了EF6 ToListAsync不运行异步但阻止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了提高我的页面性能,我想以异步方式运行一些查询。

我升级到EF6,因为它通过暴露异步方法本机支持此功能。

我无法得到查询同时启动,所以我把我的代码下载到这个简单的例子:

  var sw = new Stopwatch(); 
sw.Start();
var dummy = context.Set< CA_Event_Person>()。Take(200).ToListAsync();
sw.Stop();
Logger.Debug(attempt nr 1:+ sw.ElapsedMilliseconds);
var result = await dummy;

我的例外情况是,秒针立即停止,因为我稍后等待着。

然而,我的记录器说,秒表已经过了5000毫秒,这意味着调用是同步的,而不是异步的。


任何人都知道为什么或我可以做错了?

解决方案


任何人都知道为什么我可能做错了?


这是应用程序中EF的第一个调用吗?你可以计算EF模型的内部表示的一次性建设吗?



使用更复杂的EF模型,第一次调用有很大的开销。但是这是一个一个(每个应用程序域)的费用。



更好地拨打一些电话(然后扔掉最短和最长的时间)。 >

To increase my page performance I wanted to run a few queries asynchronously.
I upgraded to EF6 because it natively supports this feature by exposing async methods.
I couldn't get the queries to fire simultaneously so i boiled my code down to this simple example :

var sw = new Stopwatch();
sw.Start(); 
var dummy = context.Set<CA_Event_Person>().Take(200).ToListAsync();
sw.Stop();
Logger.Debug("attempt nr 1 : " + sw.ElapsedMilliseconds);
var result = await dummy;

My exception was that the stopwatch with instantly stop since i'm doing the await at a later point.
My logger however says that 5000 milliseconds have passed on the stopwatch meaning the call was performed synchronous instead of asynchronous.

Anyone got some insight in the why or what i could be doing wrong?

解决方案

Anyone got some insight in the why or what I could be doing wrong?

Is this the first call into EF in the application? Could you be timing the one-off building of the internal representation of the EF model?

With a more complex EF model there is significant overhead on the first call. But this is a one off (per app domain) cost.

Better to time a number of calls (and then throw away the shortest and longest times).

这篇关于EF6 ToListAsync不运行异步但阻止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆