是否允许在ASP.NET Core控制器中使用Task.Run? [英] Is it allowed to use Task.Run in an ASP.NET Core controller?

查看:267
本文介绍了是否允许在ASP.NET Core控制器中使用Task.Run?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:我有一个带有删除" ASP.NET Core控制器操作的Web服务.该实现包括两个步骤:第一步便宜,在此之后其他操作已经不再能看到已删除的数据,而第二步又是长时间运行,可以执行实际的删除操作.

Scenario: I have a web service with a "Delete" ASP.NET Core controller action. The implementation consists of two steps: one cheap after which other operations can already no longer see the deleted data, and a long-running second step which performs the actual deletion.

第二次操作可以使用 Task.Run 而不用等待它,以便很快返回到调用方吗?我知道此任务可能会由于应用程序关闭而丢失,但是甚至允许它使用ASP.NET Core中的 Task.Run 将工作卸载到后台,而从不等待生成的任务吗?

Is it okay to use Task.Run for the second operation and never await it and so return to the caller very soon? I know that this task could be lost due to application shutdown, but is it even allowed to offload work to the background using Task.Run in ASP.NET Core and never await the resulting task?

推荐答案

我知道此任务可能会由于应用程序关闭而丢失,但是是否甚至允许它使用ASP.NET Core中的Task.Run将工作卸载到后台,而从不等待生成的任务?

I know that this task could be lost due to application shutdown, but is it even allowed to offload work to the background using Task.Run in ASP.NET Core and never await the resulting task?

哦,当然,没有什么可以阻止这种情况的发生..NET和ASP.NET允许您使用 Task.Run ,并且响应将立即返回给用户(假设您忽略了 Task.Run 返回的任务)).

Oh, sure, nothing will prevent this. .NET and ASP.NET allow you to use Task.Run, and the response will be returned to the user without delay (assuming you ignore the task returned from Task.Run).

可以使用Task.Run进行第二次操作而从不等待它,因此很快就会返回到调用方吗?

Is it okay to use Task.Run for the second operation and never await it and so return to the caller very soon?

您说您知道该任务可能会在应用程序关闭时丢失.没错;因此完全有可能第二次删除可能永远不会发生,并且不会有任何错误或日志消息通知您该事实.此外,由于忽略了该任务,因此如果该任务失败,将不会有任何通知,因此,如果删除代码有问题,则没有日志或任何内容会通知您常规失败.如果您对此表示满意,则可以使用即弃即用功能.

You say that you're aware that the task could be lost in an app shutdown. And that's true; so it is entirely possible that the second deletion may never happen, and there will be no error or log message informing you of the fact. Furthermore, since the task is ignored, there will be no notification if that task fails, so if there's something wrong with the deletion code, no logs or anything will notify you of regular failures. If you're OK with that, then using fire-and-forget is fine.

在ASP.NET Core上,您甚至不需要 Task.Run ;您只需调用执行第二次删除操作的(异步)方法,然后忽略该任务即可,而不是 await .

On ASP.NET Core, you don't even need Task.Run; you can just call the (asynchronous) method that does the second delete operation and then ignore the task instead of awaiting it.

只需忽略任务即可工作(无论是否有 Task.Run ),但这确实具有负面影响,即ASP.NET根本不了解卸载的工作 .因此,我建议注册任务,并延迟某些时间使应用程序关闭,直到任务有机会完成.通过注册,仍然可以丢失任务,但是注册它们可以减少丢失任务的机会.

Simply ignoring tasks will work (with or without Task.Run), but this does have the negative side effect that ASP.NET isn't aware of the offloaded work at all. So I would recommend registering the task and having something delay the app shutdown until the tasks have a chance to complete. With registration, it is still possible to lose tasks, but registering them reduces the chance of losing tasks.

这篇关于是否允许在ASP.NET Core控制器中使用Task.Run?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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