4

I have couple of scenarios where I am having hard time to map the types.

Scenario 1:

 public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

Type Definition

 public class ApplicationUser : IdentityUser
    {
    }

 // Summary:
    //     Implements IUserStore using EntityFramework where TUser is the entity type
    //     of the user being stored
    //
    // Type parameters:
    //   TUser:
    public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserStore<TUser>, IDisposable where TUser : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUser
    {
 ....
  }

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
    {
....
  }

Attempted Mapping

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

            container.RegisterType(typeof(IDisposable), typeof(UserManager<>));

Error: Unable to cast object of type 'Main.Models.ApplicationUser' to type 'Microsoft.AspNet.Identity.IUserStore1[Main.Models.ApplicationUser]'. With out mapping, i get this error: The current type, Microsoft.AspNet.Identity.IUserStore1[Main.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?

Scenario 2:

 public RegisterController(
             IUserDataStorage<HandyGuy> session)
        {
            this.handySession = session;
        }

Type Definition

 public class HttpUserDataStorage<T> : IUserDataStorage<T>
  where T : class
    {
        public T Access
        {
            get { return HttpContext.Current.Session[typeof(T).FullName] as T; }
            set { HttpContext.Current.Session[typeof(T).FullName] = value; }
        }
    }

Attempted Mapping:

 container.RegisterType(typeof(IUserDataStorage<>), typeof(HttpUserDataStorage<>));

Error: No error. But I always have null in handySession.Access

HaBo
  • 13,999
  • 36
  • 114
  • 206

2 Answers2

11

Thanks to emodendroket that helped me to resolve the next mapping issue with Entity Framework.

But For my scenario, I could map the types in two ways

First one

 container.RegisterType(typeof(UserManager<>),
            new InjectionConstructor(typeof(IUserStore<>)));
            container.RegisterType<Microsoft.AspNet.Identity.IUser>(new InjectionFactory(c => c.Resolve<Microsoft.AspNet.Identity.IUser>()));
            container.RegisterType(typeof(IUserStore<>), typeof(UserStore<>));
            container.RegisterType<IdentityUser, ApplicationUser>(new ContainerControlledLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());

second one

 container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
Community
  • 1
  • 1
HaBo
  • 13,999
  • 36
  • 114
  • 206
2

If you want unity IOC in MVC5 just follow these points

  1. Create a new project asp.net mvc 5(not empty)
  2. use unity=> install-package unity.mvc
  3. WIF(windows identity framework) is broken in the app
  4. follow this excellent tutorial
  5. you need to inject all identity framework entities
Vojta
  • 1,060
  • 13
  • 11