“在创建模型时不能使用上下文” ASP.NET身份异常 [英] "Context cannot be used while the model is being created" exception with ASP.NET Identity

查看:467
本文介绍了“在创建模型时不能使用上下文” ASP.NET身份异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们打电话给AccountApiController.Register()方法时,为什么会发生这种情况?




  • 尝试使用上下文?

  • 正在尝试创建上下文?

  • 我们如何避免这种情况?

  • 我们如何调试?




消息:发生错误,



ExceptionMessage:当模型是
被创建时,不能使用上下文。如果上下文在OnModelCreating方法中使用
,或者如果相同上下文实例是多个线程并发访问的
。请注意,DbContext和相关类的实例成员
不能保证为线程
安全。,



ExceptionType:System.InvalidOperationException,



StackTrace:



在System.Web.Http.ApiController.d__1.MoveNext()



---从前一个位置引发异常的堆栈跟踪结束

$ b $在System.Runtime.CompilerServices.TaskAwaiter
的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中的

HandleNonSuccessAndDebuggerNotification(Task> task)



在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()



解决方案

问题是我们没有使用工厂模式, MS建议


您可以使用Factory实现从OWIN上下文中获取UserManager
的实例。 ...这是一个推荐的方法,获取
UserManager的每个请求的应用程序的实例。


因此,同一个上下文实例被并发访问的多个线程,因为几个请求,因此线程共享一个DbContext。



以下是正确的。 >为每次调用UserManagerFactory函数创建一个MyDbContext的新实例。

  UserManagerFactory 
=()=> ; new UserManager< IdentityUser>(new UserStore< IdentityUser>(new MyDbContext()));

以下是不正确的。看起来相似,但不会创建新的每个调用UserManagerFactory的实例。这是我们正在使用的,ergo我们的网站破产。

  var userStore = new UserStore< IdentityUser>(new MyDbContext ; 
var userManager = new UserManager< IdentityUser>(userStore);
UserManagerFactory =()=>的UserManager;


Why is this happening when we make a call to the AccountApiController.Register() method?

  • what is trying to use the context?
  • what is trying to create the context?
  • how do we avoid this?
  • how do we debug this?

"Message":"An error has occurred.",

"ExceptionMessage":"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.",

"ExceptionType":"System.InvalidOperationException",

"StackTrace":"

at System.Web.Http.ApiController.d__1.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.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

解决方案

The problem was that we were NOT using the factory pattern that MS recommends.

You can use Factory implementation to get an instance of UserManager from the OWIN context. ... This is a recommended way of getting an instance of UserManager per request for the application.

As a result, "the same context instance is accessed by multiple threads concurrently," because several requests and thus threads shared a DbContext.

This following is correct. It creates a new instance of MyDbContext for each call to the UserManagerFactory function.

UserManagerFactory 
= () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()));

The following is incorrect. It look similar but does not create a new instance for each call to UserManagerFactory. It is what we were using, ergo our site broke.

var userStore = new UserStore<IdentityUser>(new MyDbContext());                    
var userManager = new UserManager<IdentityUser>(userStore);
UserManagerFactory = () => userManager;

这篇关于“在创建模型时不能使用上下文” ASP.NET身份异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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