转换方法以使用异步 [英] Convert a method to use async

查看:75
本文介绍了转换方法以使用异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换身份验证过程以支持异步,而VS 2015 IDE则通过以下消息警告我: 异步方法缺少'await'运算符,将同步运行... 等...

I am converting a authentication process to support async and the VS 2015 IDE is warning me with the following message: The async method lacks 'await' operators and will run synchronously... etc...

无论如何,该代码将连接到LDAP存储并验证用户的帐户等. 我已经尝试了各种尝试,但是这里我只是想念一些东西.我将代码恢复到以前的样子..感谢任何指导以使其正确支持异步...

Anyway, the code connects to a LDAP store and verifies a user's account and etc... I have tried various things with await, but I am just missing something here. I put the code back to what it was before.. I would appreciate any guidance in getting it to support async correctly...

这是代码:

public async Task<User> GetAsyncADUser(PrincipalContextParameter param)
    {
        try
        {

            if (UseLDAPForIdentityServer3)
            {
                using (var pc = new PrincipalContext(ContextType.Domain, param.ADDomain, param.ADServerContainer, param.ADServerUser, param.ADServerUserPwd))
                {
                    UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(pc, param.UserNameToValidate);
                    if (userPrincipal != null)
                    {
                        bool isvalid = pc.ValidateCredentials(userPrincipal.DistinguishedName, param.UserPasswordToValidate, ContextOptions.SimpleBind);

                        if (isvalid)
                        {
                            User user = new User { ad_guid = userPrincipal.Guid.ToString(), Username = param.UserNameToValidate, Password = param.UserPasswordToValidate };
                            return user;
                        }
                    }
                }
            }

        }
        catch (Exception ex)
        {
            throw;
        }

        return null;

    }

推荐答案

来自 MSDN :

以下特征总结了构成异步方法的原因:

The following characteristics summarize what makes an async method:

  • 方法签名包括一个异步修饰符.
  • 按照惯例,async方法的名称以"Async"后缀结尾. 返回类型是以下类型之一:

  • The method signature includes an async modifier.
  • The name of an async method, by convention, ends with an "Async" suffix. The return type is one of the following types:

  • Task<TResult>,如果您的方法具有一个返回语句,其中操作数的类型为TResult.
  • Task,如果您的方法没有return语句或没有操作数的return语句.
  • Void(如果您正在编写异步事件处理程序).
  • Task<TResult> if your method has a return statement in which the operand has type TResult.
  • Task if your method has no return statement or has a return statement with no operand.
  • Void if you're writing an async event handler.

该方法通常包含至少一个await表达式,这标志着该方法在等待的异步操作完成之前无法继续的点.同时,该方法被挂起,控制权返回给该方法的调用者.本主题的下一部分说明在悬浮点发生的情况.

The method usually includes at least one await expression, which marks a point where the method can't continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method's caller. The next section of this topic illustrates what happens at the suspension point.

您可以使用return Task.Run(() => { /* your code here */ })并返回Task<User>.然后,您可以按以下方式调用此方法:

You can use return Task.Run(() => { /* your code here */ }) and return a Task<User>. Then you can call this method as :

User user = await GetAsyncADUser();

那样,您无需在方法GetAsyncADUser中使用async关键字,但是您需要使用async关键字标记使用上述代码行的方法.

That way, you don't need to use the async keyword in the method GetAsyncADUser, but you need to mark the method that uses the above line of code with the async keyword.

这篇关于转换方法以使用异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆