Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试通过图形查询从Azure AD检索照片时出现加载时间问题_C#_Azure Active Directory_Azure Ad Graph Api - Fatal编程技术网

C# 尝试通过图形查询从Azure AD检索照片时出现加载时间问题

C# 尝试通过图形查询从Azure AD检索照片时出现加载时间问题,c#,azure-active-directory,azure-ad-graph-api,C#,Azure Active Directory,Azure Ad Graph Api,我目前使用以下代码从Azure广告中获取公司目录的信息: List<QueryOption> options = new List<QueryOption>(); options.Add(new QueryOption("$filter", "accountEnabled%20eq%20true")); options.Add(new QueryOption("$select", "displayName,companyName,profilePhoto,userPrin

我目前使用以下代码从Azure广告中获取公司目录的信息:

List<QueryOption> options = new List<QueryOption>();
options.Add(new QueryOption("$filter", "accountEnabled%20eq%20true"));
options.Add(new QueryOption("$select", "displayName,companyName,profilePhoto,userPrincipalName"));

IGraphServiceUsersCollectionPage users = await gsc.Users.Request(options).GetAsync();
userResult.AddRange(users);
while (users.NextPageRequest != null)
{
    users = await users.NextPageRequest.GetAsync();
    userResult.AddRange(users);
}
这一点将页面加载时间提高到30秒以上,我在减少这一时间方面遇到了问题。因此,我在这一具体情况下的问题如下:

  • 为什么原始查询(我在选择选项中指定了profilePhoto)实际上没有提取配置文件照片信息
  • 我在这里做错了什么,造成了如此剧烈的加载时间
  • 1.为什么原始查询(我在选择选项中指定了profilePhoto)实际上没有提取配置文件照片信息? 在MicrosoftGraphAPI中,它是这样设计的。您可以使用API获取基本的用户配置文件,同时,您需要使用API获取照片

    因此,在SDK中,您将有相同的体验

    2.我在这里做错了什么,造成了如此剧烈的加载时间? 事实上,没有什么问题。但是有办法减少时间成本。正如@Matt.G在评论中所说,您可以创建一个单独的方法,返回下载照片数据的任务。然后你可以同时下载照片


    有关这方面的更多信息,您可以参考官方教程:

    考虑将foreach中的代码移动到一个方法中,该方法返回Task并执行Task.Whallon为用户创建的所有任务
    foreach(User u in userResult)
    {
        Stream photo = await gsc.Users[u.UserPrincipalName].Photo.Content.Request().GetAsync();
        byte[] buffer = new byte[16 * 1024];
    
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = photo.Read(buffer, 0, buffer.Length)) > 0)
                ms.Write(buffer, 0, read);
            imgMe.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(ms.ToArray());
        }
    }