EF6 ToListAsync does not run async but blocks the thread

ε祈祈猫儿з 提交于 2019-12-07 14:21:26

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/31287942/ef6-tolistasync-does-not-run-async-but-blocks-the-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!