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

纵饮孤独 提交于 2019-12-25 04:30:13

问题


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?

来源:https://stackoverflow.com/questions/30467185/httpserverutility-execute-blocked-while-waiting-for-an-asynchronous-operation-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!