Copied to clipboard

Flag this post as spam?

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


  • Christian Fischer 11 posts 121 karma points
    1 week ago
    Christian Fischer
    0

    SurfaceController with async await not working: Execute blocked while waiting for an asynchronous operation to complete

    Dear Umbraco Pros

    I am currently trying to integrate an async Action Result to Umbraco giving me headaches.

    The call:

    Html.RenderAction("EmbedReport", "PowerBI", new { workspaceID = new Guid("**************"), reportID = new Guid("******************") });
    

    Current Controller:

            public async Task<ActionResult> EmbedReport(Guid workspaceId, Guid reportId)
        {
            if (!m_errorMessage.IsNullOrWhiteSpace())
            {
                return View("Error", BuildErrorModel(m_errorMessage));
            }
    
            try
            {
                var embedResult = await EmbedService.GetEmbedParams(workspaceId, reportId);
                return PartialView("~/Views/Partials/PowerBI/_EmbedReport.cshtml", embedResult);
            }
            catch (HttpOperationException exc)
            {
                m_errorMessage = string.Format("Status: {0} ({1})\r\nResponse: {2}\r\nRequestId: {3}", exc.Response.StatusCode, (int)exc.Response.StatusCode, exc.Response.Content, exc.Response.Headers["RequestId"].FirstOrDefault());
                return View("Error", BuildErrorModel(m_errorMessage));
            }
            catch (Exception ex)
            {
                return View("Error", BuildErrorModel(ex.Message));
            }
        }
    

    No matter what I tried until now I always end up with some similar error message:

    System.Web.HttpException: 'Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.'
    
    InvalidOperationException: HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.
    

    Google had a few solutions but they didn't work as expected. I also read somewhere, that there will be changes in MVC6 related to this. Has anyone done this in Umbraco 8 and can give me a push in the right direction?

    Thanks and have a good Monday! Chris

  • Dan Diplo 1524 posts 6000 karma points MVP 4x c-trib
    1 week ago
    Dan Diplo
    0

    This is not really an Umbraco issue, it's just down to the fact that async partial actions are not supported in the underlying MVC5 framework. You'll probably need to wait until Umbraco 9 comes along which utilises a new framework for this to work.

    https://github.com/aspnet/AspNetWebStack/issues/61

  • Christian Fischer 11 posts 121 karma points
    4 days ago
    Christian Fischer
    0

    Dear Dan

    Thanks for the general reply. I fixed this issue now by rewriting to sync and clearing the session in between:

            public ActionResult EmbedReport(Guid workspaceId, Guid reportId)
        {
            if (!m_errorMessage.IsNullOrWhiteSpace())
            {
                return View("Error", BuildErrorModel(m_errorMessage));
            }
            try
            {
                // trick to prevent deadlocks of calling async method 
                // and waiting for on a sync UI thread.
                var syncContext = SynchronizationContext.Current;
                SynchronizationContext.SetSynchronizationContext(null);
    
                //  this is the async call, wait for the result (!)
                var embedResult = EmbedService.GetEmbedParams(workspaceId, reportId).Result;
    
                // restore the context
                SynchronizationContext.SetSynchronizationContext(syncContext);
    
                return PartialView("~/Views/Partials/PowerBI/_EmbedReport.cshtml", embedResult);
            }
            catch (HttpOperationException exc)
            {
                m_errorMessage = string.Format("Status: {0} ({1})\r\nResponse: {2}\r\nRequestId: {3}", exc.Response.StatusCode, (int)exc.Response.StatusCode, exc.Response.Content, exc.Response.Headers["RequestId"].FirstOrDefault());
                return View("Error", BuildErrorModel(m_errorMessage));
            }
            catch (Exception ex)
            {
                return View("Error", BuildErrorModel(ex.Message));
            }
        }
    

    Going strong so far. Thanks to https://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock

    Hope, this helps somebody else.

    Cheers Christian

Please Sign in or register to post replies

Write your reply to:

Draft