我们托管一个大容量的 WCF Web 服务,其逻辑上具有以下代码:
void WcfApiMethod()
{
// logic
// invoke other tasks which are critical
var mainTask = Task.Factory.StartNew(() => { /* important task */ });
mainTask.Wait();
// invoke background task which is not critical
var backgroundTask = Task.Factory.StartNew(() => { /* some low-priority background action (not entirely async) */ });
// no need to wait, as this task is best effort. Fire and forget
// other logic
}
// other APIs
Run Code Online (Sandbox Code Playgroud)
现在的问题是,在某些情况下,低优先级后台任务可能需要更长的时间(约 30 秒),例如,检测 SQL 连接问题、DB …