Search code examples
asp.net-coremicroservicesasp.net-core-webapiasp.net-core-3.1asp.net-core-identity

Unable to resolve service for type Microsoft.AspNetCore.Identity.RoleManager


I am working on project using microservice architecture, I use ASP.NET Core Identity as a separate microservice to create users and roles. I extend users and roles with custom fields and configure Identity in my API project's startup.cs. But while I run my application I got an error as following,

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microservice.IdentityMS.Application.Interfaces.IIdentityMSService Lifetime: Transient ImplementationType: Microservice.IdentityMS.Application.Services.IdentityMSService': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microservice.IdentityMS.Domain.Models.MembershipRole]' while attempting to activate 'Alexa.IdentityMS.Data.Repository.IdentityMSRepository'.)

Here's my Identity Microservice startup

Startup.cs:

using Microservice.IdentityMS.Data.Context;
using Microservice.IdentityMS.Domain.Models;
using Microservice.Infra.IoC;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Microservice.Identity.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();           
            var userConnectionString = Configuration["DbContextSettings:UserConnectionString"];
            var dbPassword = Configuration["DbContextSettings:DbPassword"];           
            var userBuilder = new NpgsqlConnectionStringBuilder(userConnectionString)
            {
                Password = dbPassword
            };

            services.AddDbContext<MembershipDBContext>(opts => opts.UseNpgsql(builder.ConnectionString));
            services.AddDbContext<UserDBContext>(opts => opts.UseNpgsql(userBuilder.ConnectionString));

            services.AddIdentity<MembershipUser, MembershipRole>(options =>
            {
                options.Password.RequiredLength = 8;
                options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+ ";
                options.SignIn.RequireConfirmedEmail = false;
            }).AddRoles<MembershipRole>().AddEntityFrameworkStores<MembershipDBContext>()
            .AddDefaultTokenProviders();            

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Identity Microservice", Version = "v1" });
            });
            services.AddMediatR(typeof(Startup));
            RegisterServices(services);
        }
        private void RegisterServices(IServiceCollection services)
        {
            DependencyContainer.RegisterServices(services);
        }
        
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity Microservice V1");
            });
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

DependencyContainer.cs:

using MediatR;
using Microservices.Domain.Core.Bus;
using Microservices.Infra.Bus;
using Microsoft.Extensions.DependencyInjection;
using Microservices.ProductMS.Domain.Interfaces;
using Microservices.ProductMS.Data.Repository;
using Microservices.ProductMS.Data.Context;
using Microservices.ProductMS.Application.Interfaces;
using Microservices.ProductMS.Application.Services;
using Microservices.PartyMS.Application.Interfaces;
using Microservices.PartyMS.Application.Services;
using Microservices.PartyMS.Domain.Interfaces;
using Microservices.PartyMS.Data.Repository;
using Microservices.PartyMS.Data.Context;
using Microservices.MasterMS.Application.Interfaces;
using Microservices.MasterMS.Application.Services;
using Microservices.MasterMS.Domain.Interfaces;
using Microservices.MasterMS.Data.Repository;
using Microservices.MasterMS.Data.Context;
using Microservices.MasterMS.Domain.Commands;
using Microservices.MasterMS.Domain.CommandHandler;
using Microservices.ProductMS.Domain.Commands;
using Microservices.ProductMS.Domain.CommandHandler;
using Microservices.PartyMS.Domain.CommandHandler;
using Microservices.AccountMS.Application.Interfaces;
using Microservices.AccountMS.Application.Services;
using Microservices.AccountMS.Domain.Interfaces;
using Microservices.AccountMS.Data.Repository;
using Microservices.AccountMS.Data.Context;
using Microservices.SalesPurchaseMS.Domain.Interfaces;
using Microservices.SalesPurchaseMS.Data.Repository;
using Microservices.SalesPurchaseMS.Data.Context;
using Microservices.SalesPurchaseMS.Application.Interfaces;
using Microservices.SalesPurchaseMS.Application.Services;
using Microservices.IdentityMS.Application.Interfaces;
using Microservices.IdentityMS.Application.Services;
using Microservices.IdentityMS.Domain.Interfaces;
using Microservices.IdentityMS.Data.Repository;
using Microservices.IdentityMS.Data.Context;
using Microsoft.AspNetCore.Identity;
using Microservices.IdentityMS.Domain.Models;

namespace Microservices.Infra.IoC
{
    public class DependencyContainer
    {
        public static void RegisterServices(IServiceCollection services)
        {
            //Domain Bus
            services.AddSingleton<IEventBus, RabbitMQBus>(sp =>
            {
                var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
                return new RabbitMQBus(sp.GetService<IMediator>(), scopeFactory);
            });

            //Subscriptions
            services.AddTransient<ProductMS.Domain.EventHandler.CompanyEventHandler>();
            services.AddTransient<PartyMS.Domain.EventHandler.CompanyEventHandler>();
            

            //Domain Events
            services.AddTransient<IEventHandler<ProductMS.Domain.Events.CompanyEvent>, ProductMS.Domain.EventHandler.CompanyEventHandler>();
            services.AddTransient<IEventHandler<PartyMS.Domain.Events.CompanyEvent>, PartyMS.Domain.EventHandler.CompanyEventHandler>();
            //services.AddTransient<IEventHandler<SalesMS.Domain.Events.CompanyEvent>, SalesMS.Domain.EventHandler.CompanyEventHandler>();
            //services.AddTransient<IEventHandler<SalesMS.Domain.Events.PartyEvent>, SalesMS.Domain.EventHandler.PartyEventHandler>();

            //Domain Commands
            services.AddTransient<IRequestHandler<CompanySyncCommand, bool>, CompanySyncCommandHandler>();
            services.AddTransient<IRequestHandler<ProductSyncCommand, bool>, ProductSyncCommandHandler>();
            services.AddTransient<IRequestHandler<ProductCategorySyncCommand, bool>, ProductCategorySyncCommandHandler>();
            services.AddTransient<IRequestHandler<PartySyncCommand, bool>, PartySyncCommandHandler>();

            //Application Services            
            services.AddTransient<IMasterService, MasterService>();
            services.AddTransient<IProductService, ProductService>();
            services.AddTransient<IPartyMSService, PartyMSService>();
            //services.AddTransient<IPurchaseService, PurchaseService>();
            services.AddTransient<IPurchaseService, PurchaseService>();
            services.AddTransient<ISaleService, SaleService>();//SaleMS
            services.AddTransient<IAccountMSService, AccountMSService>();
            services.AddTransient<IIdentityMSService, IdentityMSService>();
            services.AddTransient<IAdministrationService, AdministrationService>();

            //Data            
            services.AddTransient<IMasterRepository, MasterRepository>();
            services.AddTransient<MasterDbContext>();

            services.AddTransient<IProductRepository, ProductRepository>();
            services.AddTransient<ProductsDBContext>();
            
            services.AddTransient<IPartyMSRepository, PartyMSRepository>();
            services.AddTransient<PartyMSDBContext>();

            services.AddTransient<IPurchaseRepository, PurchaseRepository>();
            services.AddTransient<ISaleRepository, SaleRepository>();
            services.AddTransient<SPDBContext>();


            services.AddTransient<IAccountMSRepository, AccountMSRepository>();
            services.AddTransient<AccountDbContext>();

            services.AddTransient<IIdentityMSRepository, IdentityMSRepository>();
            services.AddTransient<IAdministrationRepository, AdministrationRepository>();//IdentityMS
            services.AddTransient<UserDBContext>();
        }
    }
}

IdentityMSRepository.cs:

public class IdentityMSRepository : IIdentityMSRepository
{
    private readonly UserDBContext _userContext;
    private readonly RoleManager<MembershipRole> _roleManager;
    private readonly UserManager<MembershipUser> _userManager;

    public IdentityMSRepository(UserDBContext userContext, RoleManager<MembershipRole> roleManager, UserManager<MembershipUser> userManager)
    {
        _userContext = userContext;
        _roleManager = roleManager;
        _userManager = userManager;
    }
}

What am I missing ?


Solution

  • Finally I made change as the comment suggested I moved it from DI container to startup file and it's working. @Rena Thank you so much.