I have ApplicationUserManager defined like this:
public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
}
public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password)
{
var result = base.CreateAsync(user, password);
//...
Repository.DoSomething(...); //Repository is null here
//..
}
[Dependency]
public IRepository Repository { get; set; }
}
For some reason my repository in not getting injected. Repository is always null
I also have this line in my unity configuration
.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager())
How to make it injected?
UPDATE N:
This is code from my controller; How I am getting UserManager:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
//calling my implemintation
IdentityResult result = await UserManager.CreateAsync(...);
}
My current unity config
.RegisterType<IUserStore<ApplicationUser, int>, CustomUserStore>(new HierarchicalLifetimeManager())
.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication))
.RegisterType<UserManager<ApplicationUser, int>, ApplicationUserManager>()
.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager())
.RegisterType<AccountController>(new InjectionConstructor())
.RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager())
.RegisterType<ApplicationUserManager>()
UPDATE N+1:
As I find out Unity.Mvc package is not injecting WebApi controllers. I used this method in order to inject WebApi controllers. I was trying to remove Unity.Mvc package but got Identity throwing errors. From my understanding because Unity was not able to instantiate some Identity types however they were being configured and worked in the case of setting Mvc container.
So I brought back the Unity.Mvc package to inject my Identity types. Otherwise, as I explained above, it is throwing different kind on null refs when Unity resolving Identity types.
So I have two containers now in my project Unity.Mvc injection DependencyResolver.SetResolver(new UnityDependencyResolver(container)); that needed to resolve Identity types and custom WebApi injection config.DependencyResolver = new UnityResolver(container); container that needed to inject WebApi contorllers both using same UnityConfig.
UPDATE 3:
I've changed this:
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
to this
app.CreatePerOwinContext(() => UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>());
in Startup.ConfigureAuth() and now I my WebApi container is fully working and constructing all required Identity types so I can disable Mvc container or even completely can remove Unity.Mvc package.
Thanks to @Sam Farajpour Ghamari for clerifying lots of things.