配置Unity。错误“无法访问已处置的对象。对象名称:'UserStore`1'。”

时间:2018-10-02 14:13:13

标签: c# asp.net-mvc-5 unity-container

我正在使用MVC5,EF 6和ASP.NET Identity开发应用程序。我正在使用依赖项注入Unity来解析我的类。 我的见解是这篇博客文章:https://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/

在这篇文章中做了所有事情,当我第一次运行代码时,它给了我:类型Microsoft.AspNet.Identity.EntityFramework.UserStore`1 [[AppMVC.Models.ApplicationUser,AppMVC,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]没有使用参数(ApplicationDbContext)的构造函数。在UnityConfig.RegisterTypes(IUnityContainer容器)静态方法的这一行出现错误:

public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below.
            // Make sure to add a Unity.Configuration to the using statements.
            // container.LoadConfiguration();

            // TODO: Register your type's mappings here.
            // container.RegisterType<IProductRepository, ProductRepository>();

            container.RegisterType<ApplicationDbContext>();
            container.RegisterType<ApplicationSignInManager>();
            container.RegisterType<ApplicationUserManager>();
            container.RegisterType<IUnitOfWork, UnitOfWork>();
            container.RegisterType<IBaseService<Picture>, PictureService>();
            container.RegisterType<IPictureService, PictureService>();
            container.RegisterType<IBaseService<UserExt>, UserExtService>();
            container.RegisterType<IUserExtService, UserExtService>();

            container.RegisterType<IAuthenticationManager>(
            new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
            container.RegisterType<AccountController>();
            container.RegisterType<ManageController>();
            container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
        }

特定于

container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new InjectionConstructor(typeof(ApplicationDbContext)));

之后,我将其更改为:

container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());

错误消失了。当我尝试通过我的应用程序登录并点击“登录”按钮时,我在AccountController中输入了Login方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: 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 = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}   

然后我进去

var result = await signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);

我立即结束

protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (userManager != null)
            {
                userManager.Dispose();
                userManager = null;
            }

            if (signInManager != null)
            {
                signInManager.Dispose();
                signInManager = null;
            }
        }

        base.Dispose(disposing);
    }

有错误: enter image description here

我的AccountController的构造函数如下

[Authorize]
    public class AccountController : Controller
    {
        private  ApplicationUserManager userManager;
        private  ApplicationSignInManager signInManager;
        private readonly IAuthenticationManager authenticationManager;

        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAuthenticationManager authenticationManager)
        {
            this.userManager = userManager;
            this.signInManager = signInManager;
            this.authenticationManager = authenticationManager;
        }
}

当我尝试注册新用户时,抛出相同的错误: enter image description here

有人可以告诉我代码中缺少什么吗? 谢谢你!

0 个答案:

没有答案