在控制器的异步调用的方法 [英] Calling async method in controller

查看:154
本文介绍了在控制器的异步调用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似如下的控制器:

 公共myController的:控制器
{
    公众的ActionResult DoSomething的()
    {
        CallSomeMethodWhichDoesAsyncOperations();
        返回JSON(新{成功=成功},JsonRequestBehavior.AllowGet);
    }
}

在叫我的控制器我收到以下错误:


  

这是异步操作不能在这个时间开始。异步操作
     只能异步处理器或模块或在一定期间启动
     事件在页面生命周期。如果在执行一个页面出现了这种异常,
     确保页面标记<%@页面异步=真正的%方式>


现在我没有了 CallSomeMethodWhichDoesAsyncOperations 控制和方法本身不是异步但在内部做了一些异步火灾和忘记。我能做些什么来解决这个问题?试图控制器更改为 AsyncController 和/或使控制器的异步方法。

编辑:

当我试图使用AsyncController我第一次尝试,具有相同的结果。

 公共myController的:AsyncController
{
    公众的ActionResult DoSomething的()
    {
        CallSomeMethodWhichDoesAsyncOperations();
        返回JSON(新{成功=成功},JsonRequestBehavior.AllowGet);
    }
}

然后

 公共myController的:AsyncController
{
    公共异步任务<&的ActionResult GT;做一点事()
    {
        CallSomeMethodWhichDoesAsyncOperations();
        返回JSON(新{成功=成功},JsonRequestBehavior.AllowGet);
    }
}

这确实改变了例外以下,而异步操作仍有待完成的异步模块或处理程序。


解决方案

  

现在我没有了CallSomeMethodWhichDoesAsyncOperations控制方法本身是不是异步但在内部做了一些异步火灾和忘记。我能做些什么来解决这个问题?


联系谁写它的人,使的他们的修复它。

严重的是,这是最好的选择。有没有好的定为这 - 只有一个黑客

您可以破解它像这样的工作:

 公共myController的:控制器
{
  公共异步任务<&的ActionResult GT;做一点事()
  {
    等待Task.Run(()=> CallSomeMethodWhichDoesAsyncOperations());
    返回JSON(新{成功=成功},JsonRequestBehavior.AllowGet);
  }
}

这是不推荐的。此解决方案的工作推动了一个后台线程,所以当异步操作恢复,他们将不会有的HttpContext 等,该方案完成请求虽然仍在处理工作要做。如果服务器停止/在错误的时间回收该解决方案将无法正常运行。

有只有一个妥善的解决办法:改变 CallSomeMethodWhichDoesAsyncOperations

I have a controller with something like the following:

public MyController : Controller
{
    public ActionResult DoSomething()
    {
        CallSomeMethodWhichDoesAsyncOperations();
        return Json(new { success = successful }, JsonRequestBehavior.AllowGet);
    }
}

When calling my controller I get the following error:

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

Now I dont have control over CallSomeMethodWhichDoesAsyncOperations and the method itself is not async but internally does some async fire and forget. What can I do to fix it? Have tried to change the controller to an AsyncController and/or making the method in the controller async.

Edit:

When I attempted to use an AsyncController I first tried, with the same result

public MyController : AsyncController
{
    public ActionResult DoSomething()
    {
        CallSomeMethodWhichDoesAsyncOperations();
        return Json(new { success = successful }, JsonRequestBehavior.AllowGet);
    }
}

And then

public MyController : AsyncController
{
    public async Task<ActionResult> DoSomething()
    {
        CallSomeMethodWhichDoesAsyncOperations();
        return Json(new { success = successful }, JsonRequestBehavior.AllowGet);
    }
}

Which did change the exception to the following "An asynchronous module or handler completed while an asynchronous operation was still pending."

解决方案

Now I dont have control over CallSomeMethodWhichDoesAsyncOperations and the method itself is not async but internally does some async fire and forget. What can I do to fix it?

Contact the person who wrote it and make them fix it.

Seriously, that's the best option. There's no good fix for this - only a hack.

You can hack it to work like this:

public MyController : Controller
{
  public async Task<ActionResult> DoSomething()
  {
    await Task.Run(() => CallSomeMethodWhichDoesAsyncOperations());
    return Json(new { success = successful }, JsonRequestBehavior.AllowGet);
  }
}

This is not recommended. This solution pushes off work to a background thread, so when the async operations resume, they will not have an HttpContext, etc. This solution completes the request while there is still processing to be done. This solution will not behave correctly if the server is stopped/recycled at just the wrong time.

There is only one proper solution: change CallSomeMethodWhichDoesAsyncOperations.

这篇关于在控制器的异步调用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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