C# 在控制器中调用异步方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13647346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:19:56  来源:igfitidea点击:

Calling async method in controller

c#asp.net-mvc-4async-await

提问by Christian

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" %>.

此时无法启动异步操作。异步操作只能在异步处理程序或模块内或在页面生命周期中的某些事件期间启动。如果在执行 Page 时发生此异常,请确保该 Page 标记为<%@ Page Async="true" %>

Now I dont have control over CallSomeMethodWhichDoesAsyncOperationsand 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 AsyncControllerand/or making the method in the controller async.

现在我无法控制CallSomeMethodWhichDoesAsyncOperations并且方法本身不是异步的,而是在内部执行一些异步触发并忘记。我能做些什么来修复它?已尝试将控制器更改为 anAsyncController和/或使控制器中的方法异步。

Edit:

编辑:

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

当我尝试使用我第一次尝试的 AsyncController 时,结果相同

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."

这确实将异常更改为以下“异步模块或处理程序已完成,而异步操作仍处于挂起状态”。

采纳答案by Stephen Cleary

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?

现在我无法控制 CallSomeMethodWhichDoesAsyncOperations 并且该方法本身不是异步的,但在内部进行了一些异步触发和遗忘。我能做些什么来修复它?

Contact the person who wrote it and make themfix it.

联系编写它的人并让他们修复它。

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

说真的,这是最好的选择。对此没有好的解决方法 - 只有一个 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 asyncoperations 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.

不建议这样做。此解决方案将工作推送到后台线程,因此当async操作恢复时,它们将不会有HttpContext等。此解决方案在仍有处理要做的情况下完成请求。如果服务器在错误的时间停止/回收,此解决方案将无法正常运行。

There is only one proper solution: change CallSomeMethodWhichDoesAsyncOperations.

只有一种合适的解决方案:更改CallSomeMethodWhichDoesAsyncOperations