开发者

MVC 3 - How to RedirectToAction immediately after asynchronous WCF call

How do i get my MVCcontroller method to call a long running WCF task asynchronously and redirect immediately after the asynchronously call is fired?

I have configured my W开发者_高级运维CF service reference to "Generate asynchronously operations" yet when the method is called i can see in the debugger that code steps through and passes the "RedirectToAction("RedirectToActionTestCompleted")" line but the browser does not redirect until the WCF task is finished.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

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

    public ActionResult RedirectToActionTest()
    {
        Service1Client client = new Service1Client();
        client.TestWcfCallAsync();
        return RedirectToAction("RedirectToActionTestCompleted");
    }

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

And the WCF service method

public void TestWcfCall()
{
    Thread.Sleep(30000); //30 seconds
}

Why is the web page waiting for the WCF method to complete?


The whole point of asynchronous methods is that they should not block. Your test method is not asynchronous.

Use a true async WCF call and a async controller = nothing will block.


Implement from AsyncController instead of Controller.

http://msdn.microsoft.com/en-us/library/ee728598.aspx


Use a workflow solution like WF. The page can just start the workflow and return. The execution of the WCF task can then be managed by the workflow.


If you want to redirect immediately you're not interested in WCF call result in the current http request, right? If that's true you may want to use OneWay operation call. It will still block and if you really don't want to block even on OneWay calls you can start it asyncronously - fire-and-forget scenario.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜