【问题标题】:How to execute await method in MVC如何在 MVC 中执行 await 方法
【发布时间】:2019-11-07 23:05:45
【问题描述】:

我们正在使用 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 往往会导致死锁,尤其是在 asp.net-mvc 上
  • 有什么办法解决死锁?
  • 如果要异步,则一直进行。
  • 很好的帮助@Nkosi。有效。谢谢!

标签: c# asp.net-mvc async-await


【解决方案1】:

混合使用 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 - Best Practices in Asynchronous Programming

【讨论】:

    【解决方案2】:

    最好的办法是更新您的代码以在请求链的整个过程中异步运行。你可以这样做:

    [HttpPost]
    public async Task<ActionResult> InviteUser([Bind(Include = "EmailId")] UserLogin userLogin)
    {
        if (ModelState.IsValid)
        {
            string result = await AzureADUtil.InviteNewUser(userLogin.EmailId).ConfigureAwait(false);
        }
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2012-07-14
      • 2020-06-08
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多