Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fredrik Stolpe 10 posts 103 karma points
    Dec 13, 2022 @ 14:39
    Fredrik Stolpe
    0

    How do I support async controllers actions?

    Hello

    In Umbraco 9 is seems like the only available Index action for controllers is:

    public IActionResult Index()
    

    but I want to use an async index action:

    public async Task<IActionResult> Index()
    

    in order to call async code that I have in my business logic. Else I will have to make a sync to async transition that causes bad performance and possible deadlock problems.

    I've tried making my own controller that inherits from Microsofts controller but it seems like Umbraco is unable to pick it when looking for controllers. And I assume that is because Umbraco expects the signature to be the synchronous one.

    Anyone knows if this is possible or have a suggestion on how I should do it.

    Regards Fredrik

  • Kimbrough 11 posts 141 karma points
    Dec 13, 2022 @ 18:11
    Kimbrough
    0

    "...I've tried making my own controller that inherits from Microsofts controller..."

    Depending on what you're trying to accomplish you should probably inherit from one of the Umbraco controllers and not a Microsoft controller.

    As for the code, the code below works for me using IHttpActionResult in a web api controller

    [HttpGet]
    [Route("xxx")]
    public async Task<IHttpActionResult> DoWork()
    {
        try
        {
            string result = await Request.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    
        return Ok(forwardUri);
    }
    
  • Fredrik Stolpe 10 posts 103 karma points
    Dec 14, 2022 @ 08:05
    Fredrik Stolpe
    0

    Thanks

    I want the Index method for all the controllers serving pages to be async because I have async business logic code that I want to execute.

    This worked in Umbraco 8 but in 9 I'm unable to create a controller with an index action that is async.

Please Sign in or register to post replies

Write your reply to:

Draft