当前位置:  开发笔记 > 编程语言 > 正文

ASP.NET 5 vNext依赖注入(RoleManager)

如何解决《ASP.NET5vNext依赖注入(RoleManager)》经验,为你挑选了1个好方法。

我正在尝试像UserManager一样将RoleManager传递给我的控制器,但是我有这个错误:

处理请求时发生未处理的异常.

InvalidOperationException:尝试激活'Web.MongoDBIdentitySample.Controllers.AccountController'时,无法解析类型'Microsoft.AspNet.Identity.RoleManager`1 [Web.MongoDBIdentitySample.Models.ApplicationRole]'的服务.

这是我的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.Init();

        // Add Mongo Identity services to the services container.
        services.AddIdentity()
            .AddMongoDBIdentityStores(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];              // Defaults to: Creating user collection in [Database] using [UserCollectionName] and [CollectionSettings]
                // options.RoleCollection = [IMongoCollection];              // 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()
            .AddMongoDBIdentityStores(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];              // Defaults to: Creating user collection in [Database] using [UserCollectionName] and [CollectionSettings]
                // options.RoleCollection = [IMongoCollection];              // 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();
        services.AddTransient();
    }

这是我的AccountController类:

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

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

编辑:添加了我的ApplicationRole类:

public class ApplicationUser : IdentityUser
{
}

public class ApplicationDbContext : IdentityDatabaseContext
{
}

public class ApplicationRole : IdentityRole
{

}

任何想法如何注入这个?谢谢!!



1> Kévin Chalet..:

您指定的IdentityRoleApplicationRole调用而不是您的帐户控制器正在使用的services.AddIdentity()寄存器.RoleManagerRoleManager

替换IdentityRoleApplicationRole您的ConfigureServices方法,它应该工作:

services.AddIdentity()
        .AddMongoDBIdentityStores();

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有