ConfigureAwait时不等待 [英] ConfigureAwait when not awaiting

查看:550
本文介绍了ConfigureAwait时不等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步方法,我用卸载几秒钟的价值的发射后不管的工作,以免减慢网页的加载。这项工作需要一些常规设置整齐向上的;我想要的(快)设置同步抛出,如果它抛出,但我不希望强制整洁行动在ASP上下文中运行,所以我用 ConfigureAwait 对位我在等待:

I have an async method I am using to offload a few seconds' worth of fire-and-forget work so as not to slow down my page load. This work needs a bit of general setup and tidy-up; I want the (fast) setup to throw synchronously if it throws, but I don't want to force the tidy-up to run in the ASP context so I am using ConfigureAwait on the bit I am awaiting:

public Task FireAndForget()
{
    DoSetup();
    return FireAndForgetAfterSetup();
}

private async Task FireAndForgetAfterSetup()
{
    await AFewSecondsWorthOfWork().ConfigureAwait(false);
    DoTidyUp();
}

protected void btn_Click(object sender, EventArgs e)
{
    FireAndForget();
}

这似乎很奇怪,因为


  • FireAndForgetAfterSetup 不应该真正关心它是否正在从一个ASP上下文中被调用,那么为什么它应该有是一个叫 ConfigureAwait

  • 如果我改变了主意,决定了 btn_Click 应等待 FireAndForget 来完成的,有它已经扔掉ASP的上下文(?)

  • FireAndForgetAfterSetup shouldn't really care whether or not it is being called from an ASP context, so why should it have to be the one calling ConfigureAwait?
  • If I change my mind and decide that btn_Click should wait for FireAndForget to complete, has it already thrown away the ASP context(?)

如果我误会有人能解释一下吗?

Can someone explain to me if I'm misunderstanding?

推荐答案

在ASP.NET同步上下文不允许发射后不管的工作项目是从一个请求中拉开序幕。运行时主动监控这种事,并会尝试生成异常,因为这些code模式导致空裁判,死锁,AVS和其他污秽。

The ASP.NET synchronization context doesn't allow fire-and-forget work items to be kicked off from within a request. The runtime actively monitors such things and will try to generate an exception since these code patterns lead to null refs, deadlocks, AVs, and other nastiness.

如果你绝对需要揭开序幕,在ASP.NET发射后不管的工作,可以考虑使用 WebBackgrounder 。它集成了被设计以允许此ASP.NET的可扩展性点。因此,它不会阻止活动的要求,但要记住斯蒂芬的警告:它不是以往任何时候都保证在所有被执行的 的。如果您需要保证执行,考虑可靠性机制,像服务总线。

If you absolutely need to kick off fire-and-forget work in ASP.NET, consider using WebBackgrounder. It integrates with the ASP.NET extensibility points that are designed to allow for this. So it won't block the active request, but keep in mind Stephen's caveat: it's not ever guaranteed to be executed at all. If you require guaranteed execution, consider a reliability mechanism like Service Bus.

这篇关于ConfigureAwait时不等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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