2

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.

Ross
  • 91
  • 1
  • 5
  • Show the code that calls your `List` method. Also see [Asynchronous programming with async and await](http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx) – Jim Mischel Nov 11 '13 at 13:53
  • What I'm doing is browsing to the url and\or using Fiddler to compose a GET request, I have a test project in which this approach is working OK. It was my understanding that MVC async is only async on the server so there is no need to do any awaiting on the client as it is expecting a restful API. – Ross Nov 11 '13 at 14:02
  • I usually return Json as the result of an ajax call from the client. I haven't needed to use async and the what i return is ActionResult instead of what you had (Task). See if that helps – Matt Bodily Nov 11 '13 at 15:44
  • The code snippet is the simplest form I could get and still demonstrate the issue. The List method will need to perform some http web requests in parallel requiring the controller method to be async. I know this should work because I have a much simpler test project that does return the correct JSON result. – Ross Nov 11 '13 at 16:54

1 Answers1

7

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.

Ross
  • 91
  • 1
  • 5