Skip to main content
 首页 » 编程设计

c#之UserManager.Create : An exception of type 'System.MissingMethodException' occurred in mscorlib. dll 但未在用户代码中处理

2024年02月20日16zhengyun_ustc

我目前正在 Visual Studio Express 2013 for Web 中构建一个网站,并且使用默认的成员(member)功能供用户注册。但是,当我在本地主机上打开网站并尝试注册用户时,我收到以下错误:

An exception of type 'System.MissingMethodException' occurred in mscorlib.dll  
but was not handled in user code 
 
Additional information: 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>>)'. 

这是我的浏览器的堆栈跟踪:

[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>>)'.] 
   Microsoft.AspNet.Identity.EntityFramework.UserStore`6.GetUserAggregateAsync(Expression`1 filter) +0 
   Microsoft.AspNet.Identity.EntityFramework.UserStore`6.FindByNameAsync(String userName) +1071 
   Microsoft.AspNet.Identity.UserManager`2.FindByNameAsync(String userName) +177 
   Microsoft.AspNet.Identity.<ValidateUserName>d__4.MoveNext() +803 
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 
   Microsoft.AspNet.Identity.<ValidateAsync>d__0.MoveNext() +468 
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123 
   Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +795 
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123 
   Microsoft.AspNet.Identity.<CreateAsync>d__d.MoveNext() +970 
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 
   Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +409 
   Microsoft.AspNet.Identity.UserManagerExtensions.Create(UserManager`2 manager, TUser user, String password) +260 
   Account_Register.CreateUser_Click(Object sender, EventArgs e) in c:\Users\Matthew\Documents\College\Senior\CSE 448\Capstone\Capstone\Account\Register.aspx.cs:14 
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628026 
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 

抛出异常的代码用星号标记:

protected void CreateUser_Click(object sender, EventArgs e) 
    { 
        var manager = new UserManager(); 
        var user = new ApplicationUser() { UserName = UserName.Text }; 
 
        *****IdentityResult result = manager.Create(user, Password.Text); 
        if (result.Succeeded) 
        { 
            IdentityHelper.SignIn(manager, user, isPersistent: false); 
            IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); 
        } 
        else 
        { 
            ErrorMessage.Text = result.Errors.FirstOrDefault(); 
        } 
    } 

我已经搜索了一段时间,但似乎找不到解决方案。我尝试更新所有引用资料,但无济于事。还有其他人遇到过这种情况吗?

请您参考如下方法:

明白了! FirstOrDefaultAsync 扩展方法仅存在于 .Net 4.5 Entity Framework 程序集中。我在不同的项目中引用了 .Net 4.0 和 .Net 4.5 的混合体。当我将它们全部更改为 .Net 4.5 时,它可以使用 Async 代码。