Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Graph API call hangs indefinitely

I am attempting to query Azure Active Directory User information using Microsoft Graph. I can authenticate fine but when I attempt to query user information client.Users my application hangs indefinitely: no timeout, no error, just hangs. I found this post however the suggestions there did not help me.

public bool GetUserByUniqueID(string uid, out GraphUser user)
{
    bool ret = false;
    user = new GraphUser();
    if (Authenticate(out AuthToken token))
    {
        GraphServiceClient client = GetGraphServiceClient(token);
        // The below code hangs indefinitely
        User user = client.Users[uid].Request().Select(GraphProperties).GetAsync().GetAwaiter().GetResult();
        if (user != null)
        {
            MapGraphUser(ret, user);            
            ret = true;
        }
    }
    return ret;
}

private bool Authenticate(out AuthToken token)
{
    bool ret = false;
    token = new AuthToken();
    string url = $"https://login.microsoftonline.com/{_tenant}/oauth2/v2.0/token";
    RestClient client = new RestClient(url);
    RestRequest request = new RestRequest(Method.POST);
    request.Parameters.Add(new Parameter("grant_type", _grantType, ParameterType.GetOrPost));
    request.Parameters.Add(new Parameter("scope", _scope, ParameterType.GetOrPost));
    request.Parameters.Add(new Parameter("client_secret", _clientSecret, ParameterType.GetOrPost));
    request.Parameters.Add(new Parameter("client_id", _clientId, ParameterType.GetOrPost));
    IRestResponse response = client.Execute<AuthToken>(request);
    if (response.StatusCode == HttpStatusCode.OK)
    {
        token = JsonConvert.DeserializeObject<AuthToken>(response.Content);
        ret = true;
    }
    return ret;
}

Update 5/2/2019

Reverting Microsoft.Graph and Microsoft.Graph.Core to version 1.12 allows me to call .GetAwaiter().GetResult() within a synchronous context.

Update 11/18/2020

I have refactored my code to use async/await pattern with the latest version of Microsoft.Graph and Microsoft.Graph.Core.

public async Task<GraphUser> GetUserByUniqueID(string uid)
{
    GraphUser ret = new GraphUser();    
    if (Authenticate(out AuthToken token))
    {
        GraphServiceClient client = GetGraphServiceClient(token);
        User user = await client.Users[uid].Request().Select(GraphProperties).GetAsync();
        if (user != null)
        {
            MapGraphUser(ret, user);            
            ret.Found = true;
        }
    }   
    return ret;
}
like image 893
mr.coffee Avatar asked Aug 08 '26 19:08

mr.coffee


1 Answers

I was having the same issue. I found on another article somewhere that it had something to do with two task waiting to finish at once. i cant find that article now.

For me .GetAwaiter().GetResult(); was working within a scheduled job but not as a manual button press task.

as a result of playing around with it. What worked for me was replacing .GetAwaiter().GetResult() with await. (i'm not sure why this fixed it but it did)

From:

var results = graphServiceClient.Users[uid].Request().GetAsync().GetAwaiter().GetResult();

To:

var results = await graphServiceClient.Users[uid].Request().GetAsync();

Hope this helps someone in the future

like image 129
gMoney Avatar answered Aug 10 '26 09:08

gMoney