35

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());
         }
chemitaxis
  • 13,889
  • 17
  • 74
  • 125
  • What does your ApplicationUser class look like? It is saying that it's being used as a class when it is a interface. If you are using an interface for application user, have you tried to configure DI for that interface and the implementation of it? – grimurd Jul 15 '14 at 03:00
  • Yes, I have tried, but it not work... I have some question similar: http://stackoverflow.com/questions/24731426/register-iauthenticationmanager-with-unity – chemitaxis Jul 15 '14 at 07:03

4 Answers4

50

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());
chemitaxis
  • 13,889
  • 17
  • 74
  • 125
  • 4
    (Bit late to the party). Those directive force unity to use the parameter less constructor (the InjectionConstructor-object is empty) when creating the AccountController. Otherwise Unity will try to resolve the 'most specified' constructor (and will fail). – Dribbel Oct 14 '16 at 10:20
  • 5
    Alternatively place the [InjectionConstructor] attribute on the empty constructor in AccountController. – Chris Haines Mar 13 '17 at 09:07
14

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

container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
Oladipo Olasemo
  • 2,010
  • 24
  • 31
10

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());
hvaughan3
  • 10,955
  • 5
  • 56
  • 76
5

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.

Ogglas
  • 62,132
  • 37
  • 328
  • 418