简体   繁体   English

ASP.NET身份工厂模式是否会破坏EntityFramework DbContext?

[英]Does the ASP.NET Identity Factory Pattern break the EntityFramework DbContext?

This MSDN Blog Post recommends using the Factory Pattern to get, "an instance of UserManager per request for the application." MSDN博客文章建议使用工厂模式来获取“针对应用程序的每个请求的UserManager实例”。 Meanwhile, my web application is throwing an error saying it's problematic "if the same context instance is accessed by multiple threads concurrently." 同时,我的Web应用程序抛出一个错误,指出“如果多个线程同时访问同一上下文实例,这是有问题的”。 The blog seems to contradict the exception message. 该博客似乎与异常消息矛盾。 What gives? 是什么赋予了?

The exception that I am receiving occurs on the following call: 我收到的异常发生在以下调用中:

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

Larger blog context: 更大的博客上下文:

You can use Factory implementation to get an instance of UserManager from the OWIN context. 您可以使用Factory实现从OWIN上下文中获取UserManager的实例。 This pattern is similar to what we use for getting AuthenticationManager from OWIN context for SignIn and SignOut. 此模式类似于我们用于从OWIN上下文获取AuthenticationManager的SignIn和SignOut的模式。 This is a recommended way of getting an instance of UserManager per request for the application. 这是一种推荐的方法,用于针对应用程序的每个请求获取UserManager实例。

Full exception text: 完整的例外文字:

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. 如果在OnModelCreating方法内部使用上下文,或者多个线程同时访问同一上下文实例,则可能引发此异常。 Note that instance members of DbContext and related classes are not guaranteed to be thread safe. 请注意,不能保证DbContext和相关类的实例成员是线程安全的。 "

The FactoryPattern does not break the DbContext; FactoryPattern 不会破坏DbContext。 rather, it prevents the multiple thread problem. 相反,它可以防止多线程问题。

UserManager.CreateAsync was throwing the exception because we had not properly implemented the factory pattern. UserManager.CreateAsync异常是因为我们没有正确实现工厂模式。

This following is correct. 这是正确的。 It creates a new instance of MyDbContext for each call to the UserManagerFactory function, and prevents multiple thread problems. 它为对UserManagerFactory函数的每次调用创建MyDbContext的新实例,并防止出现多线程问题。

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

The following is incorrect. 以下是不正确的。 It look similar to the above, but it does not create a new instance for each call to UserManagerFactory. 它看起来与上面相似,但是不会为UserManagerFactory的每次调用创建一个新实例。 It is what we were using, ergo we had one DbContext for the application, which meant sharing it with multiple threads concurrently, and blammo, exception. 这就是我们所使用的,因此我们为应用程序提供了一个DbContext,这意味着可以同时与多个线程以及blammo(异常)共享它。

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

I got that problem cause this: 我遇到了这个问题,原因是:

IdentityResult result = await UserManager .CreateAsync(user, model.Password); IdentityResult结果=等待UserManager .CreateAsync(user,model.Password);

UserManager was a Property of the Authorization Filter Class and not called directly from the context in the function. UserManager是授权过滤器类的属性,而不是直接从函数的上下文中调用。

Correct : var userManager = context.Get<"ApplicationUserManager">() 正确 :var userManager = context.Get <“ ApplicationUserManager”>()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM