从ASP.NET MVC行动呼吁ConfigureAwait [英] Calling ConfigureAwait from an ASP.NET MVC Action

查看:283
本文介绍了从ASP.NET MVC行动呼吁ConfigureAwait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个presentation,以为下面,因为未在正确的上下文中返回的ActionResult应该失败。我负载测试VS它并没有得到错误。我已经调试它,知道它是线程切换。因此,现在看来似乎是合法code。

请问ASP.NET不关心上下文或线程它是像一个客户端应用程序?如果是这样,在AspNetSynchronizationContext提供什么样的目的?我觉得不舒服把一个ConfigureAwait在行动本身。有什么不对劲了。任何人都可以解释一下吗?

 公共异步任务<&的ActionResult GT; AsyncWithBackendTest()
    {
        VAR的结果=等待BackendCall()ConfigureAwait(假)。
        VAR服务器= HttpContext.Server;
        HttpContext.Cache [你好] =世界;
        返回的内容(结果);
    }


解决方案

ASP.NET不具备UI线程需要很多的客户应用程序做的(由于它下面的UI框架)。这种情况下是不是线程关联,但对于跟踪页面进步(以及其他的东西,比如周围的安全环境承载的请求)

斯蒂芬Toub提到这在MSDN文章


  

Windows窗体是不是提供了一个唯一的环境
  SynchronizationContext的派生类。 ASP.NET还规定之一,
  AspNetSynchronizationContext,虽然它不是公开的并且并不意味着
  供外部使用。相反,它在幕后所用
  ASP.NET以方便在ASP.NET异步页面功能
  2.0(有关详细信息,请参阅msdn.microsoft.com/msdnmag/issues/05/10/Wicked$c$c)。这个
  实施可让ASP.NET为prevent页面处理完毕
  直到所有悬而未决的异步调用已完成


有关同步上下文中的更详细一点,从去年 斯蒂芬·克利里的文章中给出

图4特别节目,它不具备的WinForms的特定线程的行为/ WPF,但整个事情是一个伟大的阅读。


  

如果多次操作完成一次为同一个应用程序,
  AspNetSynchronizationContext将确保他们执行一次一个
  时间。他们可以执行任何线程,但该线程将有
  原始页面的身份和文化。


I was working on a presentation and thought the following should fail since the ActionResult isn't being returned on the right context. I've load tested it with VS and got no errors. I've debugged it and know that it is switching threads. So it seems like it is legit code.

Does ASP.NET not care what context or thread it is on like a client app? If so, what purpose does the AspNetSynchronizationContext provide? I don't feel right putting a ConfigureAwait in the action itself. Something seems wrong about it. Can anyone explain?

    public async Task<ActionResult> AsyncWithBackendTest()
    {
        var result = await BackendCall().ConfigureAwait(false);
        var server = HttpContext.Server;
        HttpContext.Cache["hello"] = "world";
        return Content(result);
    }

解决方案

ASP.NET doesn't have the 'UI thread' need that many clients apps do (due to the UI framework below it). That context isn't about thread affinity, but for tracking the page progress (and other things, like carrying around the security context for the request)

Stephen Toub mentions this in an MSDN article:

Windows Forms isn't the only environment that provides a SynchronizationContext-derived class. ASP.NET also provides one, AspNetSynchronizationContext, though it's not public and is not meant for external consumption. Rather, it is used under the covers by ASP.NET to facilitate the asynchronous pages functionality in ASP.NET 2.0 (for more information, see msdn.microsoft.com/msdnmag/issues/05/10/WickedCode). This implementation allows ASP.NET to prevent page processing completion until all outstanding asynchronous invocations have been completed.

A little more detail about the synchronization context is given in Stephen Cleary's article from last year.

Figure 4 in particular shows that it doesn't have the 'specific thread' behavior of WinForms/WPF, but the whole thing is a great read.

If multiple operations complete at once for the same application, AspNetSynchronizationContext will ensure that they execute one at a time. They may execute on any thread, but that thread will have the identity and culture of the original page.

这篇关于从ASP.NET MVC行动呼吁ConfigureAwait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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