2016-09-28 3 views
0

Ich habe alle Fragen zu SO durchforstet, aber ich kann nicht herausfinden, was ich hier falsch mache.Microsoft Unity DI mit Identität

Ich habe folgenden Userservice

public class UserService : BaseService 
    { 
     private readonly Func<UserManager<ApplicationUser>> _userManagerFactory; 
     private readonly Func<RoleManager<IdentityRole>> _roleManagerFactory; 

     public UserService(Func<UserManager<ApplicationUser>> userManagerFactory, Func<RoleManager<IdentityRole>> roleManagerFactory) 
     { 
      _userManagerFactory = userManagerFactory; 
      _roleManagerFactory = roleManagerFactory; 
     } 

und in meinen Konten Controller i wie folgt injizieren;

public class AccountsController : ApiController 
{ 
    [Dependency] 
    public UserService UserService { get; set; } 

Jetzt in meiner UnityConfig.cs ich die DI wie folgt einstellen;

var userInjectionConstructor = new InjectionConstructor(new MyDataContext()); 
      //container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(userInjectionConstructor); 
      container.RegisterType<UserManager<ApplicationUser>, ApplicationUserManager>(); 
      container.RegisterType<IRoleStore<IdentityRole, string>, RoleStore<IdentityRole>>(userInjectionConstructor); 

Der ApplicationUserManager ist wie folgt;

Dies kompiliert und läuft, aber ich bekomme den Fehler, wenn es versucht, die DI zu lösen;

Auflösung der Abhängigkeit ist fehlgeschlagen, type = „Microsoft.AspNet.Identity.UserManager 1[CCS.Core.ApplicationUser]", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserStore 1 [CCS.Core.ApplicationUser], eine Schnittstelle und nicht konstruiert werden kann. Sind Sie eine Typzuordnung fehlt?

Jetzt wurde dieses Thema angesprochen, denn wenn ich die folgenden verwenden, die DI Werk

var userInjectionConstructor = new InjectionConstructor(new MyDataContext()); 
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(userInjectionConstructor); 

Dann aber keiner des ApplicationUserManager Setup ist es (Token-Anbieter etc.)

.

Nun ist der ApplicationUserManager.Create Aufruf Static, also nehme ich an, ich muss dies in den Konstruktor für die DI verschieben?

Kann mir jemand erklären, was ich hier falsch mache und wie man es löst?

Antwort

Verwandte Themen