- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个演示文稿,并认为以下操作应该失败,因为 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.
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/
这个问题在这里已经有了答案: ConfigureAwait(true) not returning on the context it was awaited on (2 个答案) 关闭 2 年前。
我有一个要在 Visual Studio 中搜索的特定模式。 基本上,我想搜索包含 await 的行,但不见了 ConfigureAwait在声明的最后。 我有一些模式可以在 regex-tester
我正在开发 Blazor Server Web 应用程序,但我对使用 .ConfigureAwait(false) 感到困惑。我知道我不应该在组件代码中使用它,但我不确定嵌套代码。例如:我正在调用异步
试图了解何时应该使用 ConfigureAwait()。 据书: When an async method resumes after an await, by default it will res
我正在尝试使用 ConfigureAwait,因为我想了解它是如何工作的。 因此,我编写了以下小型控制台应用程序(实际上在 LINQPad 中运行): void Main() { // Use
我已经阅读了很多关于 async/await 编程模型的文章,仍然有一些不是很清楚的地方,我想分享一下我对这些的困惑。 假设我们有以下配置: 主要的异步方法是 public async Task Do
以为我正在处理 ConfigureAwait,然后我尝试了一个实验。 我的理解是 ConfigureAwait(false) 只有在有同步上下文的情况下才会有所作为。 ASP、WPF 等应该有上下文,
这是一些 WinForms 代码: async void Form1_Load(object sender, EventArgs e) { // on the UI thread De
我有一个在 thread1 上运行的代码。我以同步方式调用函数(使用异步方法但它不应该打扰我 - 异步方法不会将代码变为异步)。 我有一个 ConfigureAwait 设置为 false 的 awa
这个问题是在 ASP.NET WebApi 2 的上下文中提出的(不是 ASP.NET Core)。我试图对这个主题进行自己的研究,但是我找不到明确的答案。 官方MSDN documentation为
我想弄清楚是否应该在顶级请求上使用 ConfigureAwait(false)。从该主题的某种权威阅读这篇文章: http://blog.stephencleary.com/2012/07/dont-
所以我知道我通常应该在库代码中使用 ConfigureAwait(false)。异常(exception)是当我知道继续将需要调用者上下文时。但是,如果我不知道会是这样的话,我不知道该怎么办。我猜我不
如果在图书馆我有这个 public async DoSomething() { await Foo(); } 然后我将其称为 lib.DoSomething().ConfigureAwait(f
示例: static void Main(string[] args) { int counter = 0; var t = new System.Timers.Timer()
我到处都读到我应该使用 ConfigureAwait(false) 来避免死锁和出于性能原因。我们在我们的应用程序中使用 ConfigureAwait。但可能会删除 ConfigureAwaits,因
假设有这样一个方法的服务库 public async Task GetPersonAsync(Guid id) { return await GetFromDbAsync(id); } 遵循Syn
我读了这篇文章https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html - 但是我看到了一个矛盾: 我知道 UI 线程死
假设我编写了一个依赖于async 方法的库: namespace MyLibrary1 { public class ClassFromMyLibrary1 { pub
我已经阅读了一些关于 async/await 的内容,并试图通过在 UI 线程上调用 WebClient.DownloadStringTaskAsync 来重现 Windows 窗体中的死锁场景,并且
当使用 await 时,默认情况下捕获 SynchronizationContext(如果存在)并且 await 之后的代码块(延续 block )是使用该上下文执行(这会导致线程上下文切换)。 pu
我是一名优秀的程序员,十分优秀!