简体   繁体   English

MVC中的异步操作不起作用

[英]Async action in MVC does not work

I'm trying to make an async action in my async controller. 我正在尝试在我的异步控制器中执行异步操作。 All I need to do is when I press the button in the view it give me an alert indicating that the work has been sent for getting done. 我所要做的就是,当我按下视图中的按钮时,它会向我发出警报,指示已完成工作。 and when the job get's done I can do something (For example returning a json or redirect to another view). 并且当工作完成时,我可以做一些事情(例如返回json或重定向到另一个视图)。 Here is my code: 这是我的代码:

Server side: 服务器端:

    public void DoWork()
    {
       var a = MakeADelay();
    }

    public async Task<ActionResult> MakeADelay()
    {
       await Task.Delay(5000);
       return  RedirectToAction("UploadComplete");
    }

    public ActionResult UploadComplete()
    {
       return View();
    }

and this is my client side code : 这是我的客户端代码:

        $("#btnAsync").click(function () {
        $.ajax({
            url: '/Upload/DoWork',
            type: "post",
            success: function () {
                alert("Work in Progress!!");
            }
        });
    });

When this code runs all seems to execute as expected but when the server side code reaches the statement return RedirectToAction("UploadComplete"); 当此代码运行时,所有内容似乎都按预期执行,但是当服务器端代码到达该语句时, return RedirectToAction("UploadComplete"); it does nothing ! 它什么都不做! I think it runs in another thread or something. 我认为它在另一个线程或其他东西中运行。

what is the problem ? 问题是什么 ?

Second question is what does aync action actually mean ? 第二个问题是异步动作实际上是什么意思? I never want my action to await for statements to get complete. 我从不希望我的动作等待语句完成。 I always want my actions to sent jobs to execution queue and return back to client, and when the jobs has completed I can notify the client in some way. 我一直希望我的动作将作业发送到执行队列并返回给客户端,当作业完成后,我可以通过某种方式通知客户端。 How can I notify the client anyway ? 我该如何通知客户? with SignalR or something ? 用SignalR还是什么?

what is the problem ? 问题是什么 ?

It's here: 它在这里:

public void DoWork()
{
   var a = MakeADelay();
}

What this does is start MakeADelay and then immediately returns to its caller, which presumably finishes creating the response and sends it. 这样做是启动MakeADelay ,然后立即返回到其调用方,该调用方大概完成了响应的创建并发送。

After the delay completes, your MakeADelay will attempt to continue executing by calling RedirectToAction . 延迟完成后,您的MakeADelay将尝试通过调用RedirectToAction继续执行。 However, the response for that request has already been sent, so this will most likely cause an exception (which will be ignored since you never await the task a ). 但是,该请求的响应已经发送,因此很可能会导致异常(由于您从未await任务a ,因此将忽略该异常)。 Or maybe it just won't do anything, but what it certainly won't do is go back in time and rewrite the response that was already sent. 也许它什么也不会做,但是肯定不会做的是回到过去,重写已经发送的响应。

Second question is what does aync action actually mean ? 第二个问题是异步动作实际上是什么意思?

I have an introduction on the subject that you may find useful. 您可能会有用的主题进行了介绍 I also describe on my blog that async does not change the HTTP protocol - you still just get one response for every request. 我还在博客上描述了async不会更改HTTP协议 -您仍然只能为每个请求获得一个响应。 What you're trying to do is send two responses for one request, and that just won't work. 您尝试做的是针对一个请求发送两个响应,但这根本行不通。

I always want my actions to sent jobs to execution queue and return back to client, and when the jobs has completed I can notify the client in some way. 我一直希望我的动作将作业发送到执行队列并返回给客户端,当作业完成后,我可以通过某种方式通知客户端。 How can I notify the client anyway ? 我该如何通知客户? with SignalR or something ? 用SignalR还是什么?

Then you'll need a queue. 然后,您将需要一个队列。 I recommend something out-of-process with persistence guarantees. 我建议使用持久性保证进行某些过程外处理。 Eg, an Azure queue, MSMQ, WebSphere MQ, etc. 例如,Azure队列,MSMQ,WebSphere MQ等。

And, yes, you could use SignalR for notification. 而且,是的,您可以使用SignalR进行通知。 I'd recommend that. 我建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM