【发布时间】:2016-09-23 16:51:15
【问题描述】:
我正在编写一个需要在 Azure Active Directory 中创建用户的 MVC 应用程序。我已经将创建用户的代码封装在它自己的库中,它在单元测试中运行良好。但是,当在我的 MVC 控制器中调用相同的代码时,对 AddUserAsync 的调用永远不会返回。作为测试,我改变了我的控制器,以便它只是尝试获取用户而不是创建新用户。控制器看起来像:
public class RegistrationController : BaseController
{
public ActionResult Index()
{
AadAccessHelper adHelper = new AadAccessHelper();
var client = adHelper.AdClient;
AADUser aadUser = adHelper.GetUserByPrincipalName("AUser@ThisDirectory.onmicrosoft.com");
return View();
}
}
获取用户的帮助类的东西看起来像:
private async Task<User> GetUserByUserName(ActiveDirectoryClient adClient, string userName)
{
var user = await adClient.Users.Where(u => u.UserPrincipalName == userName).ExecuteSingleAsync();
return (User) user;
}
public AADUser GetUserByPrincipalName(string userName)
{
Task<User> user = GetUserByUserName(AdClient, userName);
user.Wait(new TimeSpan(0, 1, 0));
if (true == user.IsFaulted)
{
throw user.Exception;
}
if (null == user.Result)
{
return null;
}
return MapUserToAADUser(user.Result);
}
无论我给它多长时间,对 ExecuteSingleAsync 的调用都永远不会返回。我错过了什么?
测试过程是运行 Chrome 中出现的网站(我也使用了两个 IE,结果相同),我导航到注册页面(为了测试目的,我移动了代码)和那是悲伤开始的时候。
再次重申:代码在单元测试中运行时运行良好。我是否需要使用其他客户端而不是 Microsoft.Azure.ActiveDirectory.GraphClient.ActiveDirectoryClient?
完成后应用程序的目的是注册新用户并允许指定用户管理应用程序用户(重置密码、更改用户属性、将用户分配到组等)。
我在 Visual Studio 2015 中,无论最新的东西来自 GitHub。
任何帮助将不胜感激。
【问题讨论】:
-
这看起来像一个经典的死锁。看看 Stephen Cleary 的帖子:blog.stephencleary.com/2012/02/async-and-await.html
标签: c# asp.net-mvc azure-active-directory