c# - 如何在 MVC 中执行 await 方法
问题描述
我们正在使用 Microsoft Graph SDK。在控制台应用程序中实现了一个 POC,其中代码工作正常,但在 MVC 中添加此代码时它不起作用。代码卡在等待调用
从控制器调用
[HttpPost]
public ActionResult InviteUser([Bind(Include = "EmailId")] UserLogin userLogin)
{
if (ModelState.IsValid)
{
string result = AzureADUtil.InviteUser(userLogin.EmailId);
}
return View();
}
方法实现如下
public static string InviteUser(string emailAddress)
{
string result = string.Empty;
result = InviteNewUser(emailAddress).Result;
return result;
}
private static async Task<string> InviteNewUser(string emailAddress)
{
string result = string.Empty;
try
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
// Send Invitation to new user
var invitation = new Invitation
{
InvitedUserEmailAddress = emailAddress,
InviteRedirectUrl = "https://myapp.com",
SendInvitationMessage = true,
InvitedUserType = "Member"
};
// It stucks at this line
await graphClient.Invitations
.Request()
.AddAsync(invitation);
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
解决方案
混合 async-await 和阻塞代码,例如.Result
或.Wait()
倾向于导致死锁,尤其是在 asp.net-mvc 上。
如果要异步,那么就一直走。
[HttpPost]
public async Task<ActionResult> InviteUser([Bind(Include = "EmailId")] UserLogin userLogin) {
if (ModelState.IsValid) {
string result = await AzureADUtil.InviteUser(userLogin.EmailId);
}
return View();
}
随着实现也被重构为异步
public static async Task<string> InviteUser(string emailAddress)
{
string result = string.Empty;
result = await InviteNewUser(emailAddress);
return result;
}
InviteUser
现在是多余的,因为它基本上包装了私人InviteNewUser
电话。
参考Async/Await - 异步编程的最佳实践
推荐阅读
- java - 从 Angular 解析日期
- java - 为什么这个代码线程是安全的?我认为变量 testValue 是由不同的线程更新的?
- r - 如何创建一个函数来输出一个向量的每一行中有多少个字符包含在另一个向量中?
- docker - 控制 pod 的容器 IO 优先级
- ios - 重新加载数据时如何将 indexPath.row 值重置为零?
- javascript - 无法按类名定位输入字段
- javascript - Restful API:如何在 Node/Express 应用程序中显示特定数据?
- python - itertools.product 批量
- terraform-provider-gcp - Google 存储桶的 IAM 政策:google_storage_bucket_iam_binding 与 google_storage_bucket_iam_member
- python-2.7 - 我如何给 __init__ 它想要的参数?