简体   繁体   English

Microsoft Graph API 调用无限期挂起

[英]Microsoft Graph API call hangs indefinitely

I am attempting to query Azure Active Directory User information using Microsoft Graph.我正在尝试使用 Microsoft Graph 查询 Azure Active Directory 用户信息。 I can authenticate fine but when I attempt to query user information client.Users my application hangs indefinitely: no timeout, no error, just hangs.我可以很好地进行身份验证,但是当我尝试查询用户信息client.Users我的应用程序无限期挂起:没有超时,没有错误,只是挂起。 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 2019 年 5 月 2 日更新

Reverting Microsoft.Graph and Microsoft.Graph.Core to version 1.12 allows me to call .GetAwaiter().GetResult() within a synchronous context.将 Microsoft.Graph 和 Microsoft.Graph.Core 恢复到 1.12 版允许我在同步上下文中调用.GetAwaiter().GetResult()

Update 11/18/2020 2020 年 11 月 18 日更新

I have refactored my code to use async/await pattern with the latest version of Microsoft.Graph and Microsoft.Graph.Core.我重构了我的代码,以在最新版本的 Microsoft.Graph 和 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;
}

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();对我来说 .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.对我有用的用 await 替换 .GetAwaiter().GetResult() 。 (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希望这有助于将来的某人

I have fixed the issue by reverting to a previous version of Microsoft.Graph and Microsoft.Graph.Core . 我已经通过恢复到以前版本的Microsoft.GraphMicrosoft.Graph.Core来解决了这个问题。 I was using version 1.14.0 and now I am using 1.12.0. 我使用的是版本1.14.0,现在我使用的是1.12.0。 .GetAwaiter().GetResult() is broken in 1.14.0 the current release (1.15.0). .GetAwaiter().GetResult()在当前版本(1.15.0)的1.14.0中被破坏。

I'm having the same issue with NPM package of the Graph API.我在 Graph API 的 NPM 包中遇到了同样的问题。 Reverted to plain old request-promise.恢复到普通的旧请求承诺。 Now it's not stuck but does not always find the members of a group.现在它没有卡住,但并不总能找到组的成员。 Using beta version of the API works fine使用 API 的测试版工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM