Microsoft.Graph GetAsync()无限期挂起 [英] Microsoft.Graph GetAsync() hangs indefinitely

查看:110
本文介绍了Microsoft.Graph GetAsync()无限期挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发ASP.NET应用程序,除其他事项外,该应用程序应从Azure Active Directory检索用户.为此,我使用的是Microsoft Graph版本1.14.0预览库,可以在

I am developing an ASP.NET application which, among other things, is supposed to retrieve users from Azure Active Directory. For this purpose, I am using the Microsoft Graph version 1.14.0 preview library, which can be found here.

由于该库仅提供用于检索用户的异步方法,因此我正在使用以下(伪)代码来同步运行它.

As this library only provides asynchronous methods for retrieving users, I am using the following (pseudo) code to run it synchronously.

string userPrincipalName = "test.user@intuneforeducation.com";
var task = Task.Run(async () => await _graphServiceClient.Users[userPrincipalName].Request().GetAsync());

while (!task.IsCompleted)
     Thread.Sleep(200);

User retrievedUser = task.Result;

问题

我现在面临的问题是,从ASP.NET应用程序调用这段代码后,task.IsCompleted永远保持为false.现在,这是我无法绕开的奇怪部分:代码可以在控制台应用程序和单元测试(使用NUnit)中完美运行.

Problem

The problem I am facing right now is that upon calling this piece of code from the ASP.NET application, task.IsCompleted remains forever false. Now here is the strange part which I can not wrap my head around: the code runs perfectly in both a Console Application and a Unit Test (using NUnit).

人们可能认为GraphServiceClient实例在这些版本中的构建方式有所不同,但我100%肯定不是.组成它的信息是从数据库中加载的,单元测试中的代码与ASP.NET应用程序控制器中的代码完全相同.使用单元测试,上述代码将在1.5秒左右的时间内执行.在ASP.NET应用程序中,我将其运行长达30分钟,没有任何结果,没有错误,没有超时,什么也没有.

One might think that the GraphServiceClient instance is built differently in these versions, but I am 100% positive that it is not. The information that makes it up is loaded from a database, and the code in the Unit Test is exactly the same as the code in the controller of the ASP.NET application. Using the Unit Test, the above code is executed in about 1.5 seconds. In the ASP.NET application, I left it running for as long as 30 minutes without any results, no errors, no time-outs, nothing at all.

我意识到这可能是一个小问题,但我确实希望有人遇到了同样的问题并能够解决.

I realize this might be a bit of a niche problem, but I do hope that someone has run into the same problem and was able to resolve it.

我设法解决了这个问题.奇怪的是,将我所有的方法都转换为异步任务无法正常工作,甚至await都一直挂着.但是,我不完全理解为什么我的解决方案现在可以正常工作.看来我的伪代码不完全准确,解决方案就在其中.

I managed to resolve this issue. Weirdly enough, converting all my methods to async Tasks did not work, as even await kept hanging. I do however not fully understand why my solution works now. It looks as though my pseudo-code was not fully accurate, and the solution lies therein.

此代码将永久保留在while (!runTask.IsCompleted)中.

This code remains forever in while (!runTask.IsCompleted).

object GetResult<TResult>(Task<TResult> task)
{
    using (task)
    using (var runTask = Task.Run(async () => await task))
    {
        while (!runTask.IsCompleted)
            Thread.Sleep(SleepTime);

        if (runTask.Exception != null)
            throw runTask.Exception.InnerException ?? runTask.Exception;

        return runTask.Result;
    }
}

User GetUser(string userPrincipalName)
{   
    return (User)GetResult(_graphServiceClient.Users[userPrincipalName].Request().GetAsync());
}

尝试2号(无效)

此方法在执行await行后一直挂起.

Attempt #2 (does not work)

This method keeps hanging after executing the await line.

async Task<User> GetUser(string userPrincipalName)
{
    User user = await _graphServiceClient.Users[userPrincipalName].Request().GetAsync();
    return user;
}

尝试#3(有效)

此代码与尝试#1中的代码基本相同,唯一的区别是它不使用GetResult方法,但确实使用了与GetResult完全相同的方法.

Attempt #3 (works)

This code is basically the same as the code in attempt #1, the only difference being that it does not use the GetResult method, but it does use the exact same approach as GetResult.

User GetUser(string userPrincipalName)
{
    using(var task = Task.Run(async () => await _graphServiceClient.Users[userPrincipalName].Request().GetAsync()))
    {
        while (!task.IsCompleted)
            Thread.Sleep(200);

        return task.Result;
    }
}

虽然这种方法可能不被视为最佳实践,但它确实有效.我对这种方法为何有效感到非常困惑,因为尝试#1的代码无效,并且实际上是相同的代码.有人可以解释为什么吗?

While this approach might not be considered best practice, it does work. I am extremely puzzled about why this approach works, because the code in attempt #1 does not, and it is virtually the same code. Can anybody explain why that is?

推荐答案

我遇到了同样的问题(

I had the same issue (see here). I solved it by reverting Microsoft.Graph and Microsoft.Graph.Core version 1.12.0.

这篇关于Microsoft.Graph GetAsync()无限期挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆