最佳实践呼吁ConfigureAwait所有服务器端code [英] Best practice to call ConfigureAwait for all server-side code

查看:117
本文介绍了最佳实践呼吁ConfigureAwait所有服务器端code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您拥有服务器端的code(即某些 ApiController )和你的函数是异步的 - 所以他们返回任务< SomeObject> - 是最好的做法,你等着,你调用函数的任何时间 ConfigureAwait(假)

我读,这是更好的性能,因为它不具备切换线程上下文回原始线程上下文。然而,随着的ASP.NET Web API中,如果您的请求进来一个线程,你等待的一些功能,并调用 ConfigureAwait(假),它可能把你一个当你回到你的 ApiController 函数的最终结果不同的线程。

我已经输入了什么,我谈论下面的例子:

 公共类CustomerController:ApiController
{
    公共异步任务<客户>获取(INT ID)
    {
        //你是在一个特定的线程在这里
        VAR的客户=等待SomeAsyncFunctionThatGetsCustomer(ID).ConfigureAwait(假);        //现在你是在不同的线程!这会导致问题?
        回报客户;
    }
}


解决方案

此视频由ASP.NET开发团队对ASP.NET使用异步最好的信息。


  

我读,这是更好的性能,因为它不具备切换线程上下文回原始线程上下文


这是与UI应用程序,那里只有一个UI线程,你必须忠实于同步回。

在ASP.NET中,情况比较复杂一点。当异步方法恢复执行时,它将从ASP.NET线程池中的线程。如果使用禁用的背景下捕捉 ConfigureAwait(假),那么线程只是继续直接执行方法。如果你没有禁用上下文捕获,那么该线程将重新进入请求上下文,然后继续执行方法。

所以 ConfigureAwait(假)不保存您在ASP.NET线程跳;它节省您的请求上下文的重新进入,但是这通常是非常快的。 ConfigureAwait(假)可能的,如果你试图做一个请求的并行处理量小,但真正TPL是一个更好的有用适合大多数的场景。


  

然而,随着的ASP.NET Web API中,如果您的请求进来一个线程,你等待的一些功能和呼叫ConfigureAwait(假),它可能把你在不同的线程,当你正在返回的最终结果您ApiController功能。


其实,只是在做一个等待可以做到这一点。一旦你的异步法击中等待方法的被封锁,但的螺纹的返回线程池。当方法是准备继续,任何线程从线程池抢走,并用来恢复方法。

唯一的区别 ConfigureAwait 使得ASP.NET是恢复方法时,该线程是否可以进入请求上下文。

我对的SynchronizationContext MSDN文章的背景资料$ C> 和我的 异步介绍博客文章

When you have server-side code (i.e. some ApiController) and your functions are asynchronous - so they return Task<SomeObject> - is it considered best practice that any time you await functions that you call ConfigureAwait(false)?

I had read that it is more performant since it doesn't have to switch thread contexts back to the original thread context. However, with ASP.NET Web Api, if your request is coming in on one thread, and you await some function and call ConfigureAwait(false) that could potentially put you on a different thread when you are returning the final result of your ApiController function.

I've typed up an example of what I am talking about below:

public class CustomerController : ApiController
{
    public async Task<Customer> Get(int id)
    {
        // you are on a particular thread here
        var customer = await SomeAsyncFunctionThatGetsCustomer(id).ConfigureAwait(false);

        // now you are on a different thread!  will that cause problems?
        return customer;
    }
}

解决方案

This video by the ASP.NET team has the best information on using async on ASP.NET.

I had read that it is more performant since it doesn't have to switch thread contexts back to the original thread context.

This is true with UI applications, where there is only one UI thread that you have to "sync" back to.

In ASP.NET, the situation is a bit more complex. When an async method resumes execution, it grabs a thread from the ASP.NET thread pool. If you disable the context capture using ConfigureAwait(false), then the thread just continues executing the method directly. If you do not disable the context capture, then the thread will re-enter the request context and then continue to execute the method.

So ConfigureAwait(false) does not save you a thread jump in ASP.NET; it does save you the re-entering of the request context, but this is normally very fast. ConfigureAwait(false) could be useful if you're trying to do a small amount of parallel processing of a request, but really TPL is a better fit for most of those scenarios.

However, with ASP.NET Web Api, if your request is coming in on one thread, and you await some function and call ConfigureAwait(false) that could potentially put you on a different thread when you are returning the final result of your ApiController function.

Actually, just doing an await can do that. Once your async method hits an await, the method is blocked but the thread returns to the thread pool. When the method is ready to continue, any thread is snatched from the thread pool and used to resume the method.

The only difference ConfigureAwait makes in ASP.NET is whether that thread enters the request context when resuming the method.

I have more background information in my MSDN article on SynchronizationContext and my async intro blog post.

这篇关于最佳实践呼吁ConfigureAwait所有服务器端code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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