EF6 ToListAsync不运行异步但阻止线程

编程入门 行业动态 更新时间:2024-10-19 22:37:31
本文介绍了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不运行异步但阻止线程

本文发布于:2023-11-15 03:51:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   ToListAsync

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!