在ASP.NET Web API中如何执行异步的“fire and forget”操作

8

所以,我有一个Web API操作,它执行一个异步函数。

public void Post([FromBody]InsertRequest request)
{
    InsertPostCommand.Execute(request);
    DoAsync();
}

private async void DoAsync()
{
    await Task.Run(() =>
    {
        //do async stuff here
    });
}

我希望控制器动作在异步操作执行时返回给客户端。

我这样做,但是出现了异常:

System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.

请问我该怎么做?谢谢。

1
https://dev59.com/PWMm5IYBdhLWcg3wX-LT - dcastro
一个很好的问题。但是请仔细阅读所有提供的答案。在ASP.NET中,多线程编程是很棘手的。 - Agat
1个回答

14

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接