猿问

Microsoft Graph 抛出 Request_ResourceNotFound 而不是

我是一名拥有 4 个月经验的学徒,我的任务是使用 Microsoft Graph 中的数据构建假期请求应用程序。应用程序的功能之一是查找用户的经理并将其显示在仪表板上。一切都很顺利,直到我的老板登录。运行 Microsoft Graph Query 后,为了找出当前的用户管理器,Graph Api 返回错误(Request_ResourceNotFound)并破坏整个应用程序,而不是返回 null 或 0。我不知道如何处理该错误。


null如果结果是,我尝试返回null,但这没有做任何事情。


这是我的控制器所期望的:


var allUsersConnectedToCurrentManagerDisplayName = graphHelper.GetManagerForCurrentUser(userIdToCheck).DisplayName;

var allUsersConnectedToCurrentManagerEmail = graphHelper.GetManagerForCurrentUser(userIdToCheck).UserPrincipalName;

var allUsersConnectedToCurrentManagerId = graphHelper.GetManagerForCurrentUser(userIdToCheck).Id;

微软图形助手:


User GetDirectManagerForUser(GraphServiceClient _graphServiceClient, string managerId)

{


    using(var task = Task.Run(async() => await _graphServiceClient.Users[managerId].Manager.Request().GetAsync()))

    {

        while (!task.IsCompleted)

            Thread.Sleep(200);


        var manager = task.Result as Microsoft.Graph.User;


        return manager;

    }

}

我期望它返回 null 并且只是不为用户显示没有任何人在他之上的直接经理。


Helenr
浏览 82回答 2
2回答

绝地无双

所以这里发生了一些事情。第一个也是最明显的问题是您的代码连续三次从图表请求相同的用户记录。您拨打的每个电话都会GetDirectManagerForUser下载整个用户配置文件。你想避免这样做:var manager = await graphHelper.GetManagerForCurrentUser(userIdToCheck);var allUsersConnectedToCurrentManagerDisplayName = manager.DisplayName;var allUsersConnectedToCurrentManagerEmail = manager.UserPrincipalName;var allUsersConnectedToCurrentManagerId = manager.Id;要避免的第二个问题是将您的请求包装在这样的任务中。它增加了代码的复杂性,使调试变得非常困难,而且没有必要。只需在方法级别添加async Task<>,然后让编译器为您处理连接:async Task<User> GetDirectManagerForUser(GraphServiceClient _graphServiceClient, string managerId)第三,您投射结果但没有捕获任何异常(即404您得到的)。您想要捕获这些并返回一个空用户:var manager = await graphHelper.GetManagerForCurrentUser(userIdToCheck);var allUsersConnectedToCurrentManagerDisplayName = manager.DisplayName;var allUsersConnectedToCurrentManagerEmail = manager.UserPrincipalName;var allUsersConnectedToCurrentManagerId = manager.Id;async Task<User> GetDirectManagerForUser(GraphServiceClient _graphServiceClient, string managerId){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Normal path&nbsp; &nbsp; &nbsp; &nbsp; return await _graphServiceClient&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Users[managerId]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Manager&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Request()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetAsync();&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Something went wrong or no manager exists&nbsp; &nbsp; &nbsp; &nbsp; var emptyUser = new User();&nbsp; &nbsp; }}

慕婉清6462132

您必须捕获异常才能返回 null。我会这样写这个函数:    public User GetDirectManagerForUser(GraphServiceClient _graphServiceClient, string managerId)    {        //.Result, because this function in synchronious        try         {                   var manager = await _graphServiceClient.Users[managerId].Manager.Request().GetAsync().Result;            return manager;        }        catch(Exception)        {            return null;        }    } 您还可以像这样使函数异步:    public async Task<User> GetDirectManagerForUser(GraphServiceClient _graphServiceClient, string managerId)    {        try         {                   var manager = await _graphServiceClient.Users[managerId].Manager.Request().GetAsync();            return manager;        }        catch(Exception)        {            return null;        }    } 为什么没有指定可访问性级别?
随时随地看视频慕课网APP
我要回答