0

I have an asp.net mvc app with a partial view to render the User Profile Name

@{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); }

@model Inspinia_MVC5_xx.Models.UserProfile
<div class="dropdown profile-element">
    <span>
        <img alt="image" class="img-circle" src="~/Images/profile_small.jpg" />
    </span>
    <a data-toggle="dropdown" class="dropdown-toggle" href="#">
        <span class="clear">
            <span class="block m-t-xs">
                <strong class="font-bold">@Model.displayName</strong>
            </span> <span class="text-muted text-xs block">Art Director <b class="caret"></b></span>
        </span>
    </a>
    <ul class="dropdown-menu animated fadeInRight m-t-xs">
        <li><a href="@Url.Action("Profile", "AppViews")">Profile</a></li>
        <li><a href="@Url.Action("Contacts", "AppViews")">Contacts</a></li>
        <li><a href="@Url.Action("Inbox", "Mailbox")">Mailbox</a></li>
        <li class="divider"></li>
        <li><a href="@Url.Action("Login", "Pages")">Logout</a></li>
    </ul>
</div>
<div class="logo-element">
    IN+
</div>

Code:

public class UserProfileController : Controller
{
    public async Task<ActionResult> GetPropertiesForUser()
    {
        Uri serviceRoot = new Uri(SettingsHelper.AzureAdGraphApiEndPoint);
        var token = await AppToken.GetAppTokenAsync();

        ActiveDirectoryClient adClient = new ActiveDirectoryClient(
         serviceRoot,
         async () => await AppToken.GetAppTokenAsync());
        string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

        Microsoft.Azure.ActiveDirectory.GraphClient.Application app = (Microsoft.Azure.ActiveDirectory.GraphClient.Application)adClient.Applications.Where(
            a => a.AppId == SettingsHelper.ClientId).ExecuteSingleAsync().Result;
        if (app == null)
        {
            throw new ApplicationException("Unable to get a reference to application in Azure AD.");
        }

        string requestUrl = string.Format("https://graph.windows.net/{0}/users/{1}?api-version=1.5", SettingsHelper.Tenant,ClaimsPrincipal.Current.Identities.First().Name);

        HttpClient hc = new HttpClient();
        hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
            "Bearer", token);

        HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));

        if (hrm.IsSuccessStatusCode)
        {
            UserProfile up = JsonConvert.DeserializeObject<UserProfile>(await hrm.Content.ReadAsStringAsync());
            return PartialView(up);
            //return View("GetPropertiesForUser", new UserProfile
            //{
            //    DisplayName = up.displayName() //I am still missing here to get the display name only
            //});
        }
        else
        {
            return View();
        }
    }

}

However I get this error

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.

Source Error:

Line 4: Line 5:
Line 6: @{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); } Line 7:
Line 8:

How can I fix this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • Is there an `ExecuteSingle` method instead of `ExecuteSingleAsync`? – John Saunders May 26 '15 at 19:28
  • I wouldn't know. I don't know what `ExecuteSingleAsync` is. Can you link to the documentation? – John Saunders May 26 '15 at 20:03
  • I have exactly same issue, the Child RenderAction calling an async controller function causes "HttpServerUtility.Execute blocked" issue. It happened after I upgrade my project from .NET framework 4.5.2 to 4.6.2 (MVC5) I searched online and found people are saying the Child RenderAction could not be an aync function, and two years ago it is planed to be "fixed" in MVC6 (now replaced by .NET core). To me, I don't have a better solution but just change controller function to sync – Yang Jan 17 '19 at 21:43
  • Did you arrive to an answer? – qqtf Jun 24 '22 at 09:56

0 Answers0