Can't understand why Unity is not introducing Identity DbContext correctly

I'm new to Unity and Identity, and I changed my implementation of Identity 2 to use int Keys instead of strings. I followed this article . It works great at this point. However, I use Unity for IoC, and it works great with my repositories, however I struggle to make it work with personality. I went over this article to switch.

I saw similar questions, but they all used string keys, so I guess I have something funky since I use int.

The problem is in the ctor of the ApplicationUserStore class:

 public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUserStore<ApplicationUser, int>, IDisposable { public ApplicationUserStore(IdentityDb context) : base(context) { } } 

Here is IdentityDb.cs :

 public class IdentityDb : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> { public IdentityDb() : base("DefaultConnection") { } static IdentityDb() { // Set the database initializer which is run once during application start // This seeds the database with admin user credentials and admin role Database.SetInitializer<IdentityDb>(new IdentityDbInitializer()); } public static IdentityDb Create() { return new IdentityDb(); } } 

And here is the relevant part of UnityConfig.cs :

 container.RegisterType<IdentityDb>(new PerResolveLifetimeManager()); container.RegisterType<ApplicationSignInManager>(); container.RegisterType<ApplicationUserManager>(); container.RegisterType<ApplicationRoleManager>(); container.RegisterType<IAuthenticationManager>( new InjectionFactory(c=>HttpContext.Current.GetOwinContext().Authentication)); container.RegisterType<IUserStore<ApplicationUser, int>, ApplicationUserStore>( new InjectionConstructor(typeof(IdentityDb))); 

When I try to start the application, the context parameter in ctor ApplicationUserStore is null .

I would appreciate any help that could be offered.

Thanks!

+1
c # entity-framework unity-container asp.net-identity
source share
1 answer

You have an exception while allowing UserManager . But the MVC Dependency converter swallows it and returns null instead of the object.

In Startup.Auth.cs instead

 app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>()); 

write:

 app.CreatePerOwinContext(() => UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>()); //Don't forget to add Microsoft.Practices.Unity namespace 

This will cause your program to crash when Unity cannot resolve the object and throws an exception. Reading the exception information you may find a problem. If you need help in the future, just send an exception.

+1
source share

All Articles