我正在尝试通过 Google 将用户登录到我的网站。 Google 登录工作正常,但我在该行的 AccountController 方法中收到 MissingMethodException:
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
错误是:
Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(System.Linq.IQueryable`1<!!0>, System.Linq.Expressions.Expression`1<System.Func`2<!!0,Boolean>>)'.
我尝试在 AccountController 中使用 System.Threading.Tasks。我还寻找了可以使用 System.Threading.Tasks 的其他地方,并尝试将它放在那里。
问题是什么导致了这个错误,如何解决?
这是包含该行的整个方法:
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// Sign in the user with this external login provider if the user already has a login
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
case SignInStatus.Failure:
default:
// If the user does not have an account, then prompt the user to create an account
ViewBag.ReturnUrl = returnUrl;
ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
}
}
更新 在几位评论者的建议下,我一直在尝试找出 Fusion Log 发生了什么,但仍然找不到它。具体来说,我做了以下操作:我在 FusionLog 注册表中将 ForceLog 和 LogFailures 设置为 1,并给它一个目录。然后我在 11:59 建立了网站,然后等了几分钟才尝试登录 Google,这是我在 12:03 做的。
我得到了大量原始构建的日志,其中一些失败了。但是网站确实建立了,所以我认为那些一定不是致命的失败。但是当我实际尝试登录时,我只得到了一些日志,所有这些操作都是成功的。
我不会列出完整的融合日志,但这里有一个从未成功绑定(bind)到原始构建的程序集列表:
- Microsoft.VisualStudio.Web.PageInspector.Runtime
- iisexpresstray.resources
是否可能是这些原因中的任何一个导致了这个错误?
更新 2 查看其他日志,我认为 WebpageInspector.Runtime 最终确实成功绑定(bind)。唯一没有出现的是 iisexpresstray.resources,但根据一篇关于 Fusion Log 的博客,我读到:“除非您明确调试加载资源失败,否则您可能希望忽略使用文化设置为“中性”以外的其他内容的“.resources”扩展名。当 ResourceManager 探测附属程序集时,这些是预期的失败。所以现在我真的不知道失败是什么。
更新 3 其他没用的东西
- 重新安装所有软件包
- 清理和重建解决方案
更新 4
Mike Barry 告诉我 .net40 不能执行异步操作,所以如果我的任何引用引用文件的 .net40 版本,那么它就不会工作。我认为我的 EntityFramework 指的是文件的 .net 40 版本,因为它指的文件夹是 [PROJECT PATH]\packages\EntityFramework.6.1.3\lib\net40\EntityFramework.dll
。但无论我做什么,我都无法让它引用 [PROJECT PATH]\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll
。我在属性和 .csproj 文件中的所有引用都引用 .net4.5,但是当我卸载并重新安装 EntityFramework 时,它顽固地返回到 .net40 文件夹。
更新 5
我使用 try-catch 获取有关异常的所有信息。这是我得到的:
Exception System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(System.Linq.IQueryable`1<!!0>, System.Linq.Expressions.Expression`1<System.Func`2<!!0,Boolean>>)'.
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<FindAsync>d__23.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.FindAsync(UserLoginInfo login)
at Microsoft.AspNet.Identity.UserManager`2.FindAsync(UserLoginInfo login)
at Microsoft.AspNet.Identity.Owin.SignInManager`2.<ExternalSignInAsync>d__1d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at LangSite_151209.Controllers.AccountController.<ExternalLoginCallback>d__3a.MoveNext() in c:\Users\Administrator\Source\Workspaces\Lang Site 4.12\DEV\Controllers\AccountController.cs:line 342
data = System.Collections.ListDictionaryInternal
helplink =
hrresult = -2146233069
innerexception =
message = Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(System.Linq.IQueryable`1<!!0>, System.Linq.Expressions.Expression`1<System.Func`2<!!0,Boolean>>)'.
source = Microsoft.AspNet.Identity.EntityFramework
stacktrace = at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<FindAsync>d__23.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.FindAsync(UserLoginInfo login)
at Microsoft.AspNet.Identity.UserManager`2.FindAsync(UserLoginInfo login)
at Microsoft.AspNet.Identity.Owin.SignInManager`2.<ExternalSignInAsync>d__1d.MoveNext()
Soma Yarlaggadda 要求查看我的 packages.config,所以这里是:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.5.0.2" targetFramework="net451" />
<package id="EntityFramework" version="6.1.3" targetFramework="net451" />
<package id="jQuery" version="2.2.3" targetFramework="net451" />
<package id="jQuery.Validation" version="1.15.0" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
<package id="Modernizr" version="2.8.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net451" />
<package id="Owin" version="1.0" targetFramework="net451" />
<package id="Respond" version="1.4.2" targetFramework="net451" />
<package id="WebGrease" version="1.6.0" targetFramework="net451" />
</packages>
最终更新:从来没有解决过这个问题。 50 名代表为了赏金而付诸东流。我最终只是从头开始创建一个新站点并将我的内容移到那里。
最佳答案
nuget 包中的 Entity Framework 4.0 dll 不包含 FirstOrDefaultAsync 方法,因为 .NET 4.0 没有对异步和等待的 native 支持。只有 nuget 包中的 4.5 dll 具有方法调用。底线:确保解决方案中的所有项目都是为 .NET 4.5 构建的,并且它们都引用 nuget 包的 4.5 dll 文件夹。
如果您以前的项目是 4.0 并将其升级到 4.5,则 nuget 引用不会修改到 4.5 文件夹。如果您有另一个项目是 4.0 并且仍然引用 4.0 dll,那么它对 4.0 dll 的引用可以在构建时覆盖 4.5 引用。如果解决方案中的所有项目都没有引用相同的包版本和目标框架,就会发生各种奇怪的事情。检查所有这些!
关于c# - .net ExternalSigninAsync MissingMethodException 系统.Threading.tasks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36772585/