在WebApi或MVC控制器中使用ConfigureAwait(false)是否有危险? [英] Is there any danger in using ConfigureAwait(false) in WebApi or MVC controllers?

查看:131
本文介绍了在WebApi或MVC控制器中使用ConfigureAwait(false)是否有危险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两种情况:

1)WebApi控制器

    [System.Web.Http.HttpPost]
    [System.Web.Http.AllowAnonymous]
    [Route("api/registerMobile")]
    public async Task<HttpResponseMessage> RegisterMobile(RegisterModel model)
    {
        var registerResponse = await AuthUtilities.RegisterUserAsync(model, _userService, User);
        if (registerResponse.Success) {
            var response = await _userService.GetAuthViewModelAsync(model.Username, User);
            return Request.CreateResponse(HttpStatusCode.OK, new ApiResponseDto() { Success = true, Data = response });
        }
        else {
            return Request.CreateResponse(HttpStatusCode.OK, registerResponse);
        }

    }

2)MVC控制器

    [Route("public")]
    public async Task<ActionResult> Public()
    {
        if (User.Identity.IsAuthenticated)
        {
            var model = await _userService.GetAuthViewModelAsync(User.Identity.Name);
            return View("~/Views/Home/Index.cshtml", model);
        }
        else
        {
            var model = await _userService.GetAuthViewModelAsync(null);
            return View("~/Views/Home/Index.cshtml", model);
        }
    }

我一直在阅读何时应该使用ConfigureAwait,似乎应该在所有不直接与UI绑定的异步调用上使用ConfigureAwait(false).我不知道那是什么意思……在上述所有await调用中都应该使用.ConfigureAwait(false)吗?

I've been reading up on when I should use ConfigureAwait and it seems like I should use ConfigureAwait(false) on ALL of my async calls that are not tied directly to the UI. I don't know what that means though... should I be using .ConfigureAwait(false) on all of the above await calls?

我正在寻找一些明确的指导原则,以明确何时应该使用它.

I'm looking for some unambiguous guidelines around when exactly I should be using it.

此问题与最佳练习为所有服务器端代码调用ConfigureAwait -我正在WebApi和MVC的上下文中,而不是在一般的C#中,寻找关于此方法用例的简单答案.

This question is NOT the same as the Best practice to call ConfigureAwait for all server-side code - I am looking for a straightforward answer on the use-case for this method in the context of WebApi and MVC, not as general C#.

推荐答案

似乎我应该在所有未直接绑定到UI的异步调用上使用ConfigureAwait(false).

it seems like I should use ConfigureAwait(false) on ALL of my async calls that are not tied directly to the UI.

不完全是.该指导原则在这里没有意义,因为没有UI线程.

Not quite. That guideline doesn't make sense here, since there is no UI thread.

传递给ConfigureAwait的参数是continueOnCapturedContext,它可以更清楚地说明这种情况.每当该async方法的其余部分取决于当前上下文时,您都想使用ConfigureAwait(false).

The parameter passed to ConfigureAwait is continueOnCapturedContext, which explains more clearly the scenario. You want to use ConfigureAwait(false) whenever the rest of that async method does not depend on the current context.

在ASP.NET 4.x中,上下文"是请求上下文,其中包括诸如HttpContext.Current和区域性之类的内容.另外-这是未记录的部分-许多ASP.NET帮助器方法要做都依赖于请求上下文.

In ASP.NET 4.x, the "context" is the request context, which includes things like HttpContext.Current and culture. Also - and this is the undocumented part - a lot of the ASP.NET helper methods do depend on the request context.

(附带说明:ASP.NET Core不再具有上下文")

(Side note: ASP.NET Core no longer has a "context")

我应该在以上所有等待调用中使用.ConfigureAwait(false)吗?

should I be using .ConfigureAwait(false) on all of the above await calls?

我还没有听到任何坚定的指导,但是我怀疑这是可以的.

I haven't heard any firm guidance on this, but I suspect it's OK.

在我自己的代码中,我从未在控制器操作方法中使用ConfigureAwait(false),因此它们已经在请求上下文中完成了.对我来说似乎更合适.

In my own code, I never use ConfigureAwait(false) in my controller action methods, so that they complete already within the request context. It just seems more right to me.

这篇关于在WebApi或MVC控制器中使用ConfigureAwait(false)是否有危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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