Async Await MVC Action returns a task instead of an 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 the logic is executed, but instead of the HTTP Status 200 code, I get the following:

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

For testing, I simply invoke 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?

+4
source share
2 answers

ELMAH's ErrorHandlingControllerFactory returns a custom action invoker, which it uses to ensure that its HandleErrorWithElmahAttribute is applied to the controller. It never delivers an asynchronous action call, so it does not understand async / wait.

Two workarounds:

  • Try upgrading to the latest version of ELMAH - it may have fixes for async / wait.
  • Remove the ControllerBuilder.Current.SetControllerFactory(new ErrorHandlingControllerFactory()) call and just add HandleErrorWithElmahAttribute to the global filters using GlobalFilters.Filters.Add(new HandleErrorWithElmahAttribute());

Cheers, Dean

+3
source

It worked for me

changes

public class authController : Controller

to

public class authController : AsyncController

I made a controller class that contains an asynchronous action that gave this inheritance an error from the AsyncController class instead of the default controller class

I found that the answer is here

0
source

Source: https://habr.com/ru/post/1495293/


All Articles