Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Await MVC Action returning Task instead of ActionResult

I have an action as follows:

public async Task<ActionResult> Pair(string id)
    {
        try
        {
            DocuSignAPIManager docuSignAPIManager = new DocuSignAPIManager();

            DocuSignEsignatureAuthContainer authContainer = await docuSignAPIManager.Pair(User.Identity.Name, id).ConfigureAwait(false);

            DocuSignManager.Instance.Pair(User.Identity.Name, authContainer);
        }
        catch(Exception e)
        {
            ErrorManager.Instance.LogError(e);
        }

        return new HttpStatusCodeResult(200);
    }

When the action is called all of the logic is executed, however instead of an HTTP Status 200 code, I receive the following:

System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]

For testing I'm just calling the action from a web browser using the following URL:

http://localhost:20551/CharteredSurveyor/Pair/password

I really don't understand what the problem is - can anyone help?

like image 594
markpirvine Avatar asked Oct 21 '22 03:10

markpirvine


1 Answers

The ErrorHandlingControllerFactory from ELMAH returns a custom action invoker which it uses to make sure its HandleErrorWithElmahAttribute is applied to the controller. It never supplies an async action invoker so it doesn't understand async/await.

Two workarounds;

  1. Try upgrading to the latest ELMAH - it may have fixes for async/await.
  2. Remove the call to ControllerBuilder.Current.SetControllerFactory(new ErrorHandlingControllerFactory()) and simply add HandleErrorWithElmahAttribute to the global filters using GlobalFilters.Filters.Add(new HandleErrorWithElmahAttribute());

Cheers, Dean

like image 74
Dean Ward Avatar answered Oct 24 '22 08:10

Dean Ward