【问题标题】:where I need to set my await compared to the code与代码相比,我需要在哪里设置等待
【发布时间】:2016-01-01 22:28:17
【问题描述】:

我真的不知道我需要在代码中的哪个位置设置我的“等待”,因此它运行我的代码以使用户能够登录到该站点。

问题出在哪里,当用户登录网站成功时,我应该在哪里设置“等待”。

错误:

“UserLogin”不包含“GetAwaiter”的定义,也没有 扩展方法“GetAwaiter”接受类型的第一个参数 可以找到“UserLogin”(您是否缺少 using 指令或 汇编参考?)

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> login(UserLogin UserValue)
    {
        //Makes database access
        DataLinqDB db = new DataLinqDB();

        if (ModelState.IsValid)
        {
            //Is this email and also downloads "salt" from the mail.
            var email = db.brugeres.FirstOrDefault(i => i.brugernavn == UserValue.Brugernavn);
            if (email != null)
            {
                //Makes password on the SHA256
                var pass = HelperClass.Helper.Hash.getHashSha256(UserValue.Brugernavn);

                //Checker up for a username and password match up with what you have written in. 
                //Checks also while on the salt as the few from the past to the fit with "email"
                var User = db.brugeres.FirstOrDefault(u => u.brugernavn == UserValue.Brugernavn && u.adgangskode == pass && u.salt == email.salt);
                if (User != null)
                {
                    .... code her ....
                    //Sending me over to the front. (the closed portion.)

                    await UserValue; //Error here!!!
                    return RedirectToAction("index");

                }
                else
                {
                    //Error - Invalid username and password do not match.
                    ModelState.AddModelError("", "Unfortunately, it is wrong ..");
                }
            }
            else
            {
                //Error - Invalid username do not match.
                ModelState.AddModelError("", "We could not find you!");
            }
        }
        return View();
    }

错在哪里。

//Sending me over to the front. (the closed portion.)
await UserValue;//Error here!
return RedirectToAction("index");

【问题讨论】:

  • 在那个逻辑分支中,似乎没有任何东西可以等待。 UserValue 只是一个 UserLogin 对象,无需等待。
  • 实际上似乎根本没有什么可以等待正如你所写的,至少据我们所知。
  • @GlorinOakenfoot 你对你写的东西有什么看法? / 在这里给我解释一下?
  • 你只能await 可以等待的东西(错误建议有GetAwaiter 方法),通常是Task
  • await 等待异步函数的结果。 UserValue 绝对不是一个异步函数,因为它是您提供的数据。通常异步函数在名称的末尾标有“Async”。您在这里根本没有什么可以等待的,因为没有什么明显是 Async 函数。

标签: c# asp.net-mvc asynchronous


【解决方案1】:

UserValue 是您的方法参数,为什么要等待它?

您可以在查询汉堡表时使用await。您可以使用FirstOrDefaultAsync 方法,即awaitable

var email = await db.brugeres
                        .FirstOrDefaultAsync(i => i.brugernavn == UserValue.Brugernavn);
if (email != null)   
{
   //Continue with your other code (Password checking)
   //Make sure you remove the await UserValue line :)
}

FirstOrDefaultAsyncSystem.Data.Entity 命名空间中可用的扩展方法,在EntityFramework.dll 中。我检查的版本是今天最新的稳定版本,即 EntityFramework 6.1.3 如果您无法访问此扩展方法,您可能使用的是旧版本的 EF。请从nuget 更新到最新的稳定版。

【讨论】:

  • Make error 'Table' 不包含'FirstOrDefaultAsync' 的定义,并且找不到接受'Table' 类型的第一个参数的扩展方法'FirstOrDefaultAsync' (您是否缺少 using 指令或程序集引用?)
  • 您使用的是哪个版本的 EF?我认为 FirstOrDefaultAsyc 扩展方法在 EF 6 及以后版本中可用。
  • @JesperPetersen 我在询问实体框架版本,请参阅我更新的答案。
  • oohhh 好吧 :) 现在它对我有用! :) 谢谢你的帮助!!,我希望你能给我+1 :)
猜你喜欢
  • 2012-11-18
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 2015-09-06
  • 2020-10-21
相关资源
最近更新 更多