Identity Provider and Unity Dependency Injection

牧云@^-^@ 提交于 2019-12-03 14:35:25

问题


I have downloaded this sample in which I can try the features of Identity Provider in ASP.NET MVC 5:

http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

I have used many times Unity DI for my repositories, services, unit of work and other stuff without problems.

But now, when I install Unity DI, I have many problems with the Interfaces and DI that uses the Identity Provider.

I just have downloaded the code of the example and I have configured the Unity DI, but I don't want to use Unity for the Identity Membership, I want to use Unity DI just for me stuff (IRepository, IService, IUnitOfWork, etc.)

I have this error when I try to register a user:

The current type, Microsoft.AspNet.Identity.IUserStore`1[Ecoavantis.Interactive.GCI.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?

I read some post in which they said that I must include something like this, but I don't need to inject dependencies in Identity Provider...

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

Can anyone help me please?

Code Example:

 /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();
            // TODO: Register your types here


            //container.RegisterType<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());
            container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager());
            container.RegisterType<IMainPanelService, MainPanelService>();
            container.RegisterType(typeof (IRepositoryAsync<>), typeof (Repository<>));
            container.RegisterType<IDataContextAsync, ecoavantisinteractivegciContext>(new PerRequestLifetimeManager());
         }

回答1:


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());



回答2:


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

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



回答3:


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());



回答4:


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.



来源:https://stackoverflow.com/questions/24740619/identity-provider-and-unity-dependency-injection

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