加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – 从ASP.NET MVC Action调用ConfigureAwait

发布时间:2020-12-15 23:38:41 所属栏目:asp.Net 来源:网络整理
导读:我正在进行演示,并认为以下内容将失败,因为ActionResult不会在正确的上下文中返回.我加载测试它与VS并没有错误.我已经调试了,知道它是切换线程.所以似乎是合法的代码. ASP.NET不关心客户端应用程序的上下文或线程吗?如果是这样,AspNetSynchronizationContex
我正在进行演示,并认为以下内容将失败,因为ActionResult不会在正确的上下文中返回.我加载测试它与VS并没有错误.我已经调试了,知道它是切换线程.所以似乎是合法的代码.

ASP.NET不关心客户端应用程序的上下文或线程吗?如果是这样,AspNetSynchronizationContext的目的是什么?我不觉得在操作本身放置一个ConfigureAwait.有些东西似乎错了.有人可以解释吗

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

解决方法

ASP.NET没有许多客户端应用程序需要“UI线程”(由于下面的UI框架).该上下文不是关于线程亲和性,而是用于跟踪页面进度(以及其他事情,例如携带请求的安全上下文)

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
.

Stephen Cleary’s article from last year提供了有关同步上下文的更多细节.

图4特别表明它没有WinForms / WPF的“特定线程”行为,但整个事情是一个很好的阅读.

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读