2015-12-07 12 views
5

私は自分のコントローラにUserManagerのようなRoleManagerを渡すためにしようとしているが、私はこのエラーを持っている:ASP.NET 5 vNext依存関係インジェクション(RoleManager)

An unhandled exception occurred while processing the request.

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNet.Identity.RoleManager`1[Web.MongoDBIdentitySample.Models.ApplicationRole]' while attempting to activate 'Web.MongoDBIdentitySample.Controllers.AccountController'.

は、これは私のConfigureServices方法である:

// This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // Registers MongoDB conventions for ignoring default and blank fields 
     // NOTE: if you have registered default conventions elsewhere, probably don't need to do this 
     RegisterClassMap<ApplicationUser, IdentityRole, string>.Init(); 

     // Add Mongo Identity services to the services container. 
     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddMongoDBIdentityStores<ApplicationDbContext, ApplicationUser, IdentityRole, string>(options => 
      { 
       options.ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"];  // No default, must be configured if using (eg "mongodb://localhost:27017") 
       // options.Client = [IMongoClient];         // Defaults to: uses either Client attached to [Database] (if supplied), otherwise it creates a new client using [ConnectionString] 
       // options.DatabaseName = [string];         // Defaults to: "AspNetIdentity" 
       // options.Database = [IMongoDatabase];        // Defaults to: Creating Database using [DatabaseName] and [Client] 

       // options.UserCollectionName = [string];       // Defaults to: "AspNetUsers" 
       // options.RoleCollectionName = [string];       // Defaults to: "AspNetRoles" 
       // options.UserCollection = [IMongoCollection<TUser>];    // Defaults to: Creating user collection in [Database] using [UserCollectionName] and [CollectionSettings] 
       // options.RoleCollection = [IMongoCollection<TRole>];    // Defaults to: Creating user collection in [Database] using [RoleCollectionName] and [CollectionSettings] 
       // options.CollectionSettings = [MongoCollectionSettings];   // Defaults to: { WriteConcern = WriteConcern.WMajority } => Used when creating default [UserCollection] and [RoleCollection] 

       // options.EnsureCollectionIndexes = [bool];      // Defaults to: false => Used to ensure the User and Role collections have been created in MongoDB and indexes assigned. Only runs on first calls to user and role collections. 
       // options.CreateCollectionOptions = [CreateCollectionOptions];  // Defaults to: { AutoIndexId = true } => Used when [EnsureCollectionIndexes] is true and User or Role collections need to be created. 
       // options.CreateIndexOptions = [CreateIndexOptions];    // Defaults to: { Background = true, Sparse = true } => Used when [EnsureCollectionIndexes] is true and any indexes need to be created. 
      }) 
      .AddDefaultTokenProviders(); 

     services.AddIdentity<ApplicationRole, IdentityRole>() 
      .AddMongoDBIdentityStores<ApplicationDbContext, ApplicationUser, IdentityRole, string>(options => 
      { 
       options.ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"];  // No default, must be configured if using (eg "mongodb://localhost:27017") 
                              // options.Client = [IMongoClient];         // Defaults to: uses either Client attached to [Database] (if supplied), otherwise it creates a new client using [ConnectionString] 
                              // options.DatabaseName = [string];         // Defaults to: "AspNetIdentity" 
                              // options.Database = [IMongoDatabase];        // Defaults to: Creating Database using [DatabaseName] and [Client] 

       // options.UserCollectionName = [string];       // Defaults to: "AspNetUsers" 
       // options.RoleCollectionName = [string];       // Defaults to: "AspNetRoles" 
       // options.UserCollection = [IMongoCollection<TUser>];    // Defaults to: Creating user collection in [Database] using [UserCollectionName] and [CollectionSettings] 
       // options.RoleCollection = [IMongoCollection<TRole>];    // Defaults to: Creating user collection in [Database] using [RoleCollectionName] and [CollectionSettings] 
       // options.CollectionSettings = [MongoCollectionSettings];   // Defaults to: { WriteConcern = WriteConcern.WMajority } => Used when creating default [UserCollection] and [RoleCollection] 

       // options.EnsureCollectionIndexes = [bool];      // Defaults to: false => Used to ensure the User and Role collections have been created in MongoDB and indexes assigned. Only runs on first calls to user and role collections. 
       // options.CreateCollectionOptions = [CreateCollectionOptions];  // Defaults to: { AutoIndexId = true } => Used when [EnsureCollectionIndexes] is true and User or Role collections need to be created. 
       // options.CreateIndexOptions = [CreateIndexOptions];    // Defaults to: { Background = true, Sparse = true } => Used when [EnsureCollectionIndexes] is true and any indexes need to be created. 
      }) 
      .AddDefaultTokenProviders(); 

     // Add MVC services to the services container. 
     services.AddMvc(); 

     // Add application services. 
     services.AddTransient<IEmailSender, AuthMessageSender>(); 
     services.AddTransient<ISmsSender, AuthMessageSender>(); 
    } 

これは私のAccountControllerクラスです:

public class AccountController : Controller 
    { 
     private readonly UserManager<ApplicationUser> _userManager; 
     private readonly RoleManager<ApplicationRole> _roleManager; 
     private readonly SignInManager<ApplicationUser> _signInManager; 
     private readonly IEmailSender _emailSender; 
     private readonly ISmsSender _smsSender; 
     private readonly ILogger _logger; 

     public AccountController(
      UserManager<ApplicationUser> userManager, 
      RoleManager<ApplicationRole> roleManager, 
      SignInManager<ApplicationUser> signInManager, 
      IEmailSender emailSender, 
      ISmsSender smsSender, 
      ILoggerFactory loggerFactory) 
     { 
      _userManager = userManager; 
      _roleManager = roleManager; 
      _signInManager = signInManager; 
      _emailSender = emailSender; 
      _smsSender = smsSender; 
      _logger = loggerFactory.CreateLogger<AccountController>(); 
     } 
} 

EDIT:追加されました私のアプリカチオンクラス:

public class ApplicationUser : IdentityUser 
{ 
} 

public class ApplicationDbContext : IdentityDatabaseContext<ApplicationUser, ApplicationRole, string> 
{ 
} 

public class ApplicationRole : IdentityRole 
{ 

} 

これを注入する方法はありますか?ありがとう!! RoleManager<IdentityRole>しかしRoleManager<ApplicationRole>アカウントコントローラが使用されていないが登録services.AddIdentity<ApplicationUser, IdentityRole>()を呼び出すとき

答えて

8

あなたはApplicationRoleの代わりにIdentityRoleを指定しています。

があなたのConfigureServices方法でApplicationRoleIdentityRoleを交換し、それが動作するはずです:

services.AddIdentity<ApplicationUser, ApplicationRole>() 
     .AddMongoDBIdentityStores<ApplicationDbContext, ApplicationUser, ApplicationRole, string>(); 
+0

こんにちは@Pinpoint、あなた解決策は...同じエラーが動作していません。 – chemitaxis

+0

はい、私はします;) – chemitaxis

+0

あなたのコードを使用しても、DBコンテキストのApplicationRoleによって変更されたIdentityRoleが私の問題を解決しました;)ありがとうございました – chemitaxis