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());
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With