Async PartialView causes "HttpServerUtility.Execute blocked..." exception

52,083

Solution 1

Child actions must be invoked synchronously. Issue 601 I am also not aware of any recent updates to the current MVC libraries allowing this functionality.

A comment on Issue 601, hints at this functionality being added in MVC vNext, aka. MVC6. Child actions look to be replaced with ViewComponent which can be invoked asynchronously from a view as below. Full examples here and here

<div>
@await Component.InvokeAsync("YourComponent")
</div>

For more on MVC6 check out, http://www.asp.net/vnext/overview/aspnet-vnext/overview

Note: This answer is just a formality, so the question can be marked as answered

Solution 2

A workaround for this issue is to make the method synchronious as required by MVC, clear the SynchronizationContext, call the async method and wait for the result, then restore the context.

see my full aswer here

Solution 3

I had similar issue calling API for some social media website and fetching our last few posts and displaying them at the home page of one of our websites.

the work around this was to use JavaScript to fetch the data I want to display at the home page from the View (Or partial view you wish to display) and then use ID.innerHTML + = to hook the fetched data inside the home page as shown at the following code

 <div id="ID"></div>

<script>
    var ID = document.getElementById('twitter');

    var sendHttpRequest = (method, url) => {
        var promise = new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest();

            xhr.open(method, url);

            xhr.onload = () => {
                resolve(xhr.response);
            };

            xhr.send();
        });

        return promise;
    };

    window.addEventListener('load', function () {
        sendHttpRequest('GET', '////////////// the URL for the View (Or Partial View) /////////').then((responseData) => {ID.innerHTML += responseData})

    })
</script>

Hope this will help someone who's looking for a faster solution for this issue.

Thanks

Share:
52,083
BrunoLM
Author by

BrunoLM

I'm a Developer for Fun! Things I like Code Play games Anime / Manga Contact information [email protected] LinkedIn Facebook Site - https://brunolm.github.io/ Blog - http://blog.codingwise.com/

Updated on July 27, 2021

Comments

  • BrunoLM
    BrunoLM almost 3 years

    I have a partial view that tries to retrieve a IEnumerable<Post> from the database using async...

    Method

    public static class PostService
    {
        public static int PostsPerPage = 50;
    
        public static async Task<IEnumerable<Post>> GetRecentAsync(int page = 0)
        {
            return await entityFrameworkDbContext.Posts
                .ToListAsync();
        }
    }
    

    PartialView

    public async Task<ActionResult> Recent(int page = 0)
    {
        return PartialView(await PostService.GetRecentAsync(page));
    }
    

    And then if I try to call it

    @Html.Action("Recent", "Post")
    

    I get the following exception

    HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.

    Why do I get this error? Shouldn't it work?

  • sɐunıɔןɐqɐp
    sɐunıɔןɐqɐp almost 6 years
    (This post does not seem to provide a quality answer to the question. Please either edit your answer, or just post it as a comment to the question).
  • serge
    serge almost 5 years
    the 601 link is not valid anymore
  • Kevin Giszewski
    Kevin Giszewski about 2 years
    If you're stuck with an MVC4 solution, making the partial render via an ajax call solved this for me.