问题描述
下面是我的问题:我需要调用一个ApiController内的多个第三方方法。这些方法的签名是任务DoSomethingAsync(SomeClass的someData,SomeOtherClass MOREDATA)
。我想这些电话继续在后台运行时,ApiController已发送的数据返回给客户端后。当 DoSomethingAsync
完成我想要做一些记录,也许一些数据保存到文件系统。我该怎么办呢?我想preFER使用asyny /等待语法。
Here's my problem: I need to call multiple 3rd party methods inside an ApiController. The signature for those methods is Task DoSomethingAsync(SomeClass someData, SomeOtherClass moreData)
. I want those calls to continue running in the background, after the ApiController has sent the data back to the client. When DoSomethingAsync
completes I want to do some logging and maybe save some data to the file system. How can I do that? I'd prefer to use the asyny/await syntax.
推荐答案
斯蒂芬描述了为什么基本上开始长期运行的内部的ApiController是一个坏主意,发射后不管的任务。
Stephen described why starting essentially long running fire-and-forget tasks inside an ApiController is a bad idea.
也许你应该创建一个单独的服务执行这些发射后不管的任务。这的服务的可能是不同的ApiController,队列后面的工人,凡是能在自己主持,并有一个独立的生命周期。
Perhaps you should create a separate service to execute those fire-and-forget tasks. That service could be a different ApiController, a worker behind a queue, anything that can be hosted on its own and have an independent lifetime.
这将使得不同的任务生命周期更容易管理,并从ApiController的核心职责分离的长期运行的任务的关注。
This would make management of the different task lifetimes much easier and separate the concerns of the long-running tasks from the ApiController's core responsibilities.
这篇关于在的WebAPI长期运行的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!