简体   繁体   English

C#异步MVC方法返回ContentResult System.Threading.Tasks.Task`1 [System.Web.Mvc.JsonResult]

[英]C# Async MVC method returns ContentResult System.Threading.Tasks.Task`1[System.Web.Mvc.JsonResult]

I'm trying to convert some methods to async and have started off with a simple example within the controller I am modifying: 我正在尝试将一些方法转换为异步方法,并以我正在修改的控制器内的一个简单示例开始:

public class MyAPIController : AsyncController
{
    public async Task<JsonResult> List()
    {
        return Json(123456, JsonRequestBehavior.AllowGet);
    }

    ...
}

When I test the method rather than the Json result I get the string value "System.Threading.Tasks.Task`1[System.Web.Mvc.JsonResult]" which I have verified with Fiddler and by browsing. 当我测试方法而不是Json结果时,我得到了字符串值“ System.Threading.Tasks.Task`1 [System.Web.Mvc.JsonResult]”,该值已通过Fiddler并通过浏览进行了验证。

The project has been manually upgraded to .NET45. 该项目已手动升级到.NET45。 Searches suggest that this is possibly a problem with incorrect assembly versions but a check of the project file and Process view suggests that the correct types are referenced and loaded at run time. 搜索表明这可能是程序集版本不正确的问题,但是对项目文件和“过程”视图的检查表明,在运行时已引用并加载了正确的类型。

Does anyone have any debugging tips for this? 有人对此有任何调试提示吗?

Thanks. 谢谢。

After stripping out 90% of the code to get it working and gradually adding it back in until it failed it turns out that a custom ControllerActionInvoker was being used. 剥离90%的代码以使其正常运行并逐步将其重新添加到失败之前,结果表明正在使用自定义ControllerActionInvoker。 The fix was to change the custom invoker to inherit from AsyncControllerActionInvoker and override BeginInvokeAction instead of InvokeAction. 解决方法是将自定义调用程序更改为从AsyncControllerActionInvoker继承,并覆盖BeginInvokeAction而不是InvokeAction。 AsyncControllerActionInvoker can handle both Async and sync methods. AsyncControllerActionInvoker可以处理Async和sync方法。

暂无
暂无

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

相关问题 C# 异步任务<string>结果为 System.Threading.Tasks.Task 1[System.String] - C# async Task<string> result as System.Threading.Tasks.Task 1[System.String] “System.Threading.Tasks.Task`1 [System.Web.Mvc.ActionResult]”显示而不是查看 - “System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]” displayed instead of view 无法将类型'System.Threading.Tasks.Task <System.Web.Mvc.ActionResult>'隐式转换为'System.Web.Mvc.ActionResult' - Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Web.Mvc.ActionResult>' to 'System.Web.Mvc.ActionResult' 找不到 System.Threading.Tasks.Task 方法 - System.Threading.Tasks.Task Method Not Found 找不到方法:'System.Threading.Tasks.Task`1<!--!0--> - Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Web.Mvc.JsonResult&#39;不包含&#39;MaxJsonLength的定义 - System.Web.Mvc.JsonResult' does not contain a definition for 'MaxJsonLength 无法将类型&#39;System.Web.Mvc.RedirectToRouteResult&#39;隐式转换为&#39;System.Web.Mvc.JsonResult&#39; - Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.JsonResult' 无法将类型System.web.mvc.JsonResult隐式转换为System.Generic.IEnumerable - Cannot implicitly Convert type System.web.mvc.JsonResult to System.Generic.IEnumerable 方法错误“无法从等待和异步属性中等待” System.Threading.Tasks.Task” - Method Error 'Cannot await 'System.Threading.Tasks.Task' from await and async properties 无法将类型“Microsoft.AspNetCore.Mvc.NotFoundResult”隐式转换为“System.Threading.Tasks.Task” - Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.NotFoundResult' to 'System.Threading.Tasks.Task'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM