調(diào)用ConfigreA的最佳實(shí)踐-等待所有服務(wù)器端代碼當(dāng)您有服務(wù)器端代碼(即ApiController),并且您的函數(shù)是異步的,所以它們返回Task<SomeObject>-是否認(rèn)為最佳做法是,在任何時(shí)候等待你調(diào)用的函數(shù)ConfigureAwait(false)?我已經(jīng)讀到,它具有更高的性能,因?yàn)樗恍枰獙⒕€程上下文切換回原來(lái)的線程上下文。但是,使用ASP.NETWebApi,如果您的請(qǐng)求出現(xiàn)在一個(gè)線程上,并且等待某個(gè)函數(shù)并調(diào)用ConfigureAwait(false),這可能會(huì)使您在返回您的最終結(jié)果時(shí)處于不同的線程中。ApiController功能。我已經(jīng)打了一個(gè)例子來(lái)說(shuō)明我在下面說(shuō)的話: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;
}
3 回答

白衣非少年
TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
<system.web> <globalization culture="en-AU" uiCulture="en-AU" /> ...
// CultureInfo.CurrentCulture ~ {en-AU}await xxxx.ConfigureAwait(false);// CultureInfo.CurrentCulture ~ {en-US}
- 3 回答
- 0 關(guān)注
- 515 瀏覽
添加回答
舉報(bào)
0/150
提交
取消