如何执行异步“发射后不管”中的ASP.NET Web API操作 [英] How to execute async 'fire and forget' operation in ASP.NET Web API

查看:457
本文介绍了如何执行异步“发射后不管”中的ASP.NET Web API操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有此Web API动作,即执行异步函数。

So, I have this Web API action, that executes an asynchronous function.

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

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

其实我是想控制器动作返回给客户端,同时异步操作执行。

I actually want the controller action to return to the client whilst the async operation executes.

我这样做,我从而获得一个例外:

I did this and I am getting an exception thus:

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

我怎样才能做到这一点,请??

How can I achieve this please ??

感谢

推荐答案

你所要求做的是<一个href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx\">extremely危险。我有一个博客文章解释了为什么这是一个坏主意,包括$ C $下一个 BackgroundTaskManager 键入的东西会去可怕的错误的几率降至最低。不幸的是,即使你注册ASP.NET运行时的工作背景,它仍然是一个坏主意,依赖此行为。

What you are asking to do is extremely dangerous. I have a blog post that explains why this is a bad idea, including code for a BackgroundTaskManager type that minimizes the chance that something will go horribly wrong. Unfortunately, even when you register your background work with the ASP.NET runtime, it's still a bad idea to depend on this behavior.

的妥善解决是让你的WebAPI要求地方的请求到一个可靠的队列(如天青队列),并拥有独立的服务(如Azure的工作者角色)的排队过程。

The proper solution is to have your WebAPI request place a request into a reliable queue (such as Azure Queue), and have an independent service (such as an Azure Worker Role) process that queue.

这篇关于如何执行异步“发射后不管”中的ASP.NET Web API操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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