Async Index method in block controller

ZZ
ZZ
Vote:
 

Hi,

   I am trying to implement async call in block controller. 

The method inside Index method calls the Api endpoint. 

When making it async, is throws inner exception "InvalidOperationException: HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete."

It is causing a performance issue for the webiste, as we are having 4-5 blocks on page

[HttpGet]
        public async Task<ActionResult> Index(xBlock currentBlock, xyViewModel viewModel)
        {
            var xxx = await xxx.IndexAsync();
            var yyy= await yyy.Index2Async();

         return PartialView(viewModel);
}

How can I make block methods async ? I have tried a lot but still getting same error

ASP.Net version-> 5.2.7

#225593
Jul 21, 2020 9:11
Vote:
 

You can't do that. A block is technically a child action of the page. And child actions cannot be asynchronous.

If you are attempting this because you are calling an asynchronous-only API, you can run it synchronously with an AsyncHelper.

#225594
Jul 21, 2020 9:20
ZZ
Vote:
 

Stefan thanks for quick reply. I just want to know what could be the best strategy to perfrom async because right now i am calling Api synchoronous, due to the restriction on child actions,  

We are calling blocks async but inside blocks we are right now calling API synchoronous which are causing performance issue, It there a better way to handle it ? 

 <script>
        $('#asynchoronousBlock@(Model.BlockField)')
            .load('@xxx.yyy(string.Format("/CallBlockAsync?Id={0}", Model.BlockField))',
                function (respText, statusText, xhr) {
                    if (statusText === "error") {
                        LogError();
                    }
                });
    </script>
[HttpGet]
        public ActionResult Index(xBlock currentBlock, xyViewModel viewModel)
        {
            var xxx = xxx.IndexAsync();
            var yyy=  yyy.Index2Async();
            Task.WhenAll(xxx, yyy).Wait();

         return PartialView(viewModel);
}
#225595
Jul 21, 2020 9:47
Vote:
 

If you do the heavy loading in a WebAPI controller, then you can do fully asynchronous actions.

The block controller will then only render the markup and script synchronously. And you won't need to have a custom route to the block controller.

But you can't return markup from the WebAPI. So you will need to get data from the API and render it to markup on the client.

#225597
Edited, Jul 21, 2020 10:27
ZZ
Vote:
 

Hi Stefan thanks for your input. Are you aware of any newer EPiServer version or ASP.Net where child actions can be made asynchronous. 

#225604
Jul 21, 2020 11:35
Stefan Holm Olsen - Jul 21, 2020 12:33
No. You would need MVC 6 or newer for that.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.