【问题标题】:vNext. AspNet.Identity and custom UserStore. UserStore disposed exceptionv下一个。 AspNet.Identity 和自定义 UserStore。 UserStore 处理异常
【发布时间】:2014-10-23 21:49:22
【问题描述】:

我正在尝试理解 vNext。
我编写了自定义 UserStore,它与 MongoDB 一起使用并实现了这些接口:

  public class UserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserSecurityStampStore<ApplicationUser>,
    IUserLoginStore<ApplicationUser>, IUserClaimStore<ApplicationUser>, IUserEmailStore<ApplicationUser>, IUserRoleStore<ApplicationUser>,
    IUserTwoFactorStore<ApplicationUser>

在 Startup.cs 添加:

app.UseServices(services =>
        {

            services.AddIdentity<ApplicationUser>()
                .AddUserStore(() => { return new UserStore(); })
                .AddUserManager<UserManager<ApplicationUser>>()
                .AddHttpSignIn();

            services.AddMvc();
        });

然后尝试使用 Visual Studio 模板中未更改的 AccountController 并遇到问题。
登录时,我在 UserStore.FindByNameAsync() 中收到 ObjectDisposedException - 称为 UserStore.Dispose()。
在 github.com/aspnet Store.Dispose() 上的 UserManager 代码中,仅在 UserManager.Dispose() 中调用。
我可以忽略 Dispose 的调用,一切正常,但这不是好方法。
所以我不知道我该怎么办

附:问题是:什么(以及为什么)可以调用 UserStore.Dispose()?

【问题讨论】:

    标签: asp.net-mvc asp.net-identity asp.net-core objectdisposedexception asp.net-identity-3


    【解决方案1】:

    在 vNext 中,DI 内置并管理身份服务的生命周期。您可能会在服务被释放后尝试使用身份,默认情况下,身份服务的生命周期仅限于请求,因此,例如,如果您尝试挂起对用户管理器的引用并在多个请求中重用它,那会导致 ObjectDisposedException。

    【讨论】:

    • UserManager 我从构造函数参数 AccountController(UserManager, SignInManager) 收到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2014-07-29
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多