我们在azure上有一个活动目录的office365帐户。我正在做一个搜索功能,用给定的搜索词查询我们的Azure AD。由于Graph API仅支持.StartsWith(string) LINQ查询,因此我必须引入所有组,然后使用我的搜索词查询该集合。
我正在使用this demo的'Get Group members‘功能作为我的搜索功能的指南。
下面是我的代码:
public List<myModel> SearchGroups(string term)
{
List<myModel> returnList = new List<myModel>();
//my service root uri
Uri serviceRoot = new Uri(serviceRootURL);
//create the client and get authorization
ActiveDirectoryClient adClient = new ActiveDirectoryClient(serviceRoot, async () => await GetAppTokenAsync());
//get collection of IGroup
IPagedCollection<IGroup> groups = adClient.Groups.ExecuteAsync().Result;
//do while loop because groups are returned in paged list...
do
{
List<IGroup> directoryObjects = groups.CurrentPage.ToList();
//get groups that contain the search term
foreach (IGroup item in directoryObjects.Where(x=>x.DisplayName.ToLower().Contains(term.ToLower())))
{
returnList.Add(new myModel(item as Microsoft.Azure.ActiveDirectory.GraphClient.Group));
}
//get next page of results
groups = groups.GetNextPageAsync().Result;
} while (groups.MorePagesAvailable); //also tried while(groups != null) same issue
return returnList;
}如果我让它运行,代码就会挂起,永远不会返回任何东西,如果我暂停它,它通常会停在下面这一行
groups = groups.GetNextPageAsync().Result;如果我在代码中放置一个断点并逐步执行,它会工作得很好,所以我认为这是异步方法的问题。我只是没有使用异步方法的经验,而且在我看来,图形api文档也不是很好,所以我被卡住了。
使用: azure MVC、C#、ASP.NET活动目录图API、Web
发布于 2015-09-03 02:04:08
请不要使用.Result(),它很容易发生死锁!在这种情况下,只需对异步调用使用await关键字即可。如果你使用异步编程,那么你应该让整个调用都是异步的。尝试使某些东西异步、同步是一种糟糕的做法
https://stackoverflow.com/questions/32194170
复制相似问题