Identity Provider and Unity Dependency Injection

有些话、适合烂在心里 提交于 2019-12-03 04:20:21

Ok, I have resolved my problem, I have injected the dependencies with this method, but I don't understand very well why it's work...

Now, Identity works fine with my Unity DI

container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<RolesAdminController>(new InjectionConstructor());
container.RegisterType<ManageController>(new InjectionConstructor());
container.RegisterType<UsersAdminController>(new InjectionConstructor());

In my case I added the following to UnityConfig.cs in the RegisterComponents Method.

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

Thanks chemitaxis! Since I do not enough rep to comment, I am posting a new answer. I am assuming chemitaxis created the last 3 controllers on his own because simply registering the first AccountController seems to have worked for me, even though I extended the User model properties:

container.RegisterType<AccountController>(new InjectionConstructor());

Update for chemitaxis and hvaughan3 answer. Since you say it happens when you try to register a user it is probably in the standard AccountController. The original problem for this is that Unity tries to call the constructor with two parameters, for example:

public AccountController(
    ApplicationUserManager userManager,
    ApplicationSignInManager signInManager)
{
    UserManager = userManager;
    SignInManager = signInManager;
}

The following line will tell Unity to call the parameterless constructor instead and won't need anything else.

container.RegisterType<AccountController>(new InjectionConstructor());

I hope this explanation helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!