【发布时间】:2015-08-25 01:23:41
【问题描述】:
我们在 azure 上有一个带有活动目录的 office365 帐户。 我正在制作一个搜索功能,使用给定的搜索词查询我们的 Azure AD。由于 Graph API 仅支持 .StartsWith(string) LINQ 查询,因此我必须拉入所有组,然后使用我的搜索词查询该集合。
我正在使用this demo 中的“获取群组成员”功能作为创建搜索功能的指南。
这是我的代码:
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 文档并不是那么好,所以我被卡住了。
使用:ASP.NET MVC、C#、Azure Active Directory Graph API、Web API
【问题讨论】:
-
在异步方法上使用
.Result调用来强制同步不是一个好主意。将您的方法设为async并使用await groups.GetNextPageAsync()会有多大影响? -
@EdgySwingsetAcid 此方法在 API 控制器上(我在我的普通 MVC 控制器中创建我的 API 控制器的实例并以这种方式调用该方法)这是否意味着我必须使我的常规 MVC 控制器异步也一样?
标签: c# linq azure active-directory azure-active-directory