gpt4 book ai didi

asp.net-mvc-4 - 从 ASP.NET MVC 操作调用 ConfigureAwait

转载 作者:行者123 更新时间:2023-12-03 14:59:35 25 4
gpt4 key购买 nike

我正在做一个演示文稿,并认为以下操作应该失败,因为 ActionResult 没有在正确的上下文中返回。我已经用 VS 对其进行了负载测试,没有出现任何错误。我已经调试过了,知道它正在切换线程。所以看起来它是合法的代码。

ASP.NET 是否像客户端应用程序一样不关心它所在的上下文或线程?如果是这样,AspNetSynchronizationContext 提供什么目的?我觉得在 Action 本身中放置一个 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.

关于asp.net-mvc-4 - 从 ASP.NET MVC 操作调用 ConfigureAwait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com