2016-09-28 4 views
0

私はすべての質問を辿ってきましたが、ここで間違っていることを解決できないようです。IDを持つMicrosoft Unity DI

私は、次の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; 
     } 

と、次のように私は私の注入アカウントコントローラ内を持っています。

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

私のUnityConfig.csでは、次のようにDIを設定しました。

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); 

ここで、ApplicationUserManagerは次のとおりです。

public class ApplicationUserManager : UserManager<ApplicationUser> 
{ 
    public ApplicationUserManager(IUserStore<ApplicationUser> store) 
     : base(store) 
    { 
    } 

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var container = (IUnityContainer)Startup.HttpConfiguration.DependencyResolver.GetService(typeof(IUnityContainer)); 
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new MyDataContext())); 
     // Configure validation logic for usernames 
     manager.UserValidator = new UserValidator<ApplicationUser>(manager) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = true 
     }; 
     // Configure validation logic for passwords 
     manager.PasswordValidator = new PasswordValidator 
     { 
      RequiredLength = 6, 
      RequireNonLetterOrDigit = true, 
      RequireDigit = true, 
      RequireLowercase = true, 
      RequireUppercase = true, 
     }; 
     var dataProtectionProvider = options.DataProtectionProvider; 
     if (dataProtectionProvider != null) 
     { 
      manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
     } 
     return manager; 
    } 
} 

これはコンパイルして実行しますが、DIを解決しようとするとエラーが発生します。依存関係の

解決に失敗しました、タイプ=「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]、インタフェースであり、構築することができません。あなたは型マッピングを逃していますか?

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

を次にDIは、しかし、ApplicationUserManagerセットアップのどれもが(トークンプロバイダーなど)が存在しない作品

;私は、次の使用している場合

は今、この問題は提起されました。

ApplicationUserManager.Create呼び出しが静的なので、これをDIのコンストラクタに移動する必要があると仮定します。

誰も私がここで間違っていると説明する方法を説明することができますか?

答えて

関連する問題