简体   繁体   English

EF中这两个异步调用有什么区别?

[英]what is the difference between this two async call in EF?

I have seen a new feature in EF6, the async methods. 我在EF6中看到了一个新功能,即异步方法。 I find an example. 我找到了一个例子。

This first way is the normal call, with EF5 for example: 第一种方式是普通呼叫,例如EF5:

public Store FindClosestStore(DbGeography location)
{
    using (var context = new StoreContext())
    {
        return (from s in context.Stores
           orderby s.Location.Distance(location)
           select s).First();
    }
}

And the new call, with async method in EF6. 和新的调用,在EF6中使用异步方法。

public async Task<Store> FindClosestStore(DbGeography location)
{
    using (var context = new StoreContext())
    {
        return await (from s in context.Stores
            orderby s.Location.Distance(location)
            select s).FirstAsync();
    }
}

However, I can do the following (the syntaxis is aprox, I do it by memory): 但是,我可以执行以下操作(语法是aprox,我是通过内存执行的):

public async Task<Store> MyAsyncMethod(DbGeography location)
{
     return await Task.Run(() => FindClosestStore());
}

I mean, that I can use Task.Run to call the first method, that is no async, to wait the result. 我的意思是,我可以使用Task.Run调用第一个方法,即没有异步,等待结果。 At the moment, is the way that I use to call async any method, not only EF. 目前,是我用来调用异步任何方法的方式,而不仅仅是EF。 This is an async call too or the truly async call is when I use the EF6 async method? 这也是异步调用,或者当我使用EF6异步方法时真正的异步调用?

Why the needed of the async methods in the new version of EF6? 为什么在新版本的EF6中需要异步方法? Only for simplicity? 只是为了简单?

The difference here is how the code awaits. 这里的不同之处在于代码如何等待。

In the of this code: 在这段代码中:

public async Task<Store> FindClosestStore(DbGeography location)
{
    using (var context = new StoreContext())
    {
        return await (from s in context.Stores
            orderby s.Location.Distance(location)
            select s).FirstAsync();
    }
}

EF will issue a query against the database, and return. EF将对数据库发出查询,然后返回。

Once the result have returned, the task will complete and the await block will continue to execute. 结果返回后,任务将完成,await块将继续执行。

That is, there is no thread in .NET itself that is waiting for the response. 也就是说,.NET本身没有等待响应的线程。 There is (hopefully) a lower level callback from the db driver that notifies .NET when the result has arrived. (希望)有一个来自db驱动程序的低级回调,它会在结果到达时通知.NET。

(This is at least how other async IO works in .NET, and I assume the same for ADO.NET async) (这至少是.NET中其他异步IO的工作方式,我假设ADO.NET异步相同)

In the other case: 在另一种情况下:

public async Task<Store> MyAsyncMethod(DbGeography location)
{
     return await Task.Run(()=> FindClosestStore());
}

There will be a thread waiting for the response from the DB. 将有一个线程等待来自DB的响应。 that is, you will have blocking IO but it will be hidden from the consumer with your task.run trick. 也就是说,你将拥有阻止IO,但它会被你的task.run技巧隐藏起来。

Both cases will behave the same for the consumer, the difference is that you are hogging up resources in the last example. 这两种情况对于消费者而言都是相同的,不同之处在于您在最后一个示例中占用了资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM