简体   繁体   中英

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.

The project has been manually upgraded to .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. The fix was to change the custom invoker to inherit from AsyncControllerActionInvoker and override BeginInvokeAction instead of InvokeAction. AsyncControllerActionInvoker can handle both Async and sync methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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