'How to remove error ' System.AggregateException: 'Some services are not able to be constructed'' '?

i am getting this error when i run the application:

System.AggregateException HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: KimiaBaseProject.Framework.Commands.CommandHandler1[KimiaBaseProject.Core.Domain.Courses.Commands.AddCourseCommand] Lifetime: Scoped ImplementationType: KimiaBaseProject.Core.ApplicationServices.Courses.Commands.AddCourseCommandHandler': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet1[KimiaBaseProject.Core.Domain.Courses.Entities.Course]' while attempting to activate 'KimiaBaseProject.Infrastructures.Data.SqlServer.Courses.Repositories.CourseCommandRepository'.) (Error while validating the service descriptor 'ServiceType: KimiaBaseProject.Core.Domain.Courses.Repositories.ICourseCommandRepository Lifetime: Transient ImplementationType: KimiaBaseProject.Infrastructures.Data.SqlServer.Courses.Repositories.CourseCommandRepository': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet1[KimiaBaseProject.Core.Domain.Courses.Entities.Course]' while attempting to activate 'KimiaBaseProject.Infrastructures.Data.SqlServer.Courses.Repositories.CourseCommandRepository'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at KimiaBaseProject.Endpoints.WebUI.Program.Main(String[] args) in C:\Users\n.eskandari\Desktop\NedaUnitSelection_3\BaseProject_Core3.1\03. EndPoints\KimiaBaseProject.Endpoints.WebUI\Program.cs:line 17 This exception was originally thrown at this call stack: [External Code] Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: KimiaBaseProject.Framework.Commands.CommandHandler1[KimiaBaseProject.Core.Domain.Courses.Commands.AddCourseCommand] Lifetime: Scoped ImplementationType: KimiaBaseProject.Core.ApplicationServices.Courses.Commands.AddCourseCommandHandler': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet1[KimiaBaseProject.Core.Domain.Courses.Entities.Course]' while attempting to activate 'KimiaBaseProject.Infrastructures.Data.SqlServer.Courses.Repositories.CourseCommandRepository'. Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet1[KimiaBaseProject.Core.Domain.Courses.Entities.Course]' while attempting to activate 'KimiaBaseProject.Infrastructures.Data.SqlServer.Courses.Repositories.CourseCommandRepository'.

The Startup.cs is:

    namespace KimiaBaseProject.Endpoints.WebUI
    {
        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.AddLocalization(options => options.ResourcesPath = "Resources");
    
                 .AddEntityFrameworkStores<CommandDbContext>()
                 .AddErrorDescriber<CustomIdentityErrorDescriber>();
    
                services.AddScoped<IdentityErrorDescriber, CustomIdentityErrorDescriber>();
    
                #endregion
      
    
    
                services.AddControllersWithViews()
                     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                    .AddDataAnnotationsLocalization(options =>
                    {
                        options.DataAnnotationLocalizerProvider = (type, factory) =>
                            factory.Create(typeof(SharedResource));
                    })
                       .AddNewtonsoftJson(options =>
                       {
                           options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    
                           options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                       });
    
                
                services.AddKendo();
                services.AddSignalR();
                services.AddHttpClient();
               services.AddMemoryCache();
              
                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
                services.AddDbContext<CommandDbContext>(ServiceLifetime.Transient);
                services.AddScoped<QueryDbContext>();
                services.AddScoped<IUnitOfWork, CommandDbContext>();
                services.AddTransient<IResourceManager, ResourceManager<SharedResource>>();
    
    
               
    
                services.AddAutoMapperConfigService();
                services.AddCommandHandlersRegistery(typeof(CommandHandler<>));
                services.AddCustomServicesRegistery();
         
    
            }
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<User> userManager)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                    app.UseHttpsRedirection();
    
                }
                app.UseStaticFiles();
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
    
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller}/{action}/{id?}");
    
                    endpoints.MapControllerRoute(
                       name: "area",
                       pattern: "{area:exists}/{controller}/{action}/{id?}");
                });
                ApplicationDbInitializer.SeedDefaultUser(userManager);
                if (env.IsDevelopment())
                {
                    app.UseSpa(spa =>
                    {
                        spa.Options.SourcePath = "AppUi";
                        spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
                    });
                }
                else
                {
                    app.UseSpa(spa =>
                    {
                        spa.Options.SourcePath = "AppUi";
                        app.Use(async (context, next) =>
                        {
                            context.Request.Path = "/dist/index.html";
                            await next();
                        });
                    });
                }
    
               
                app.UseCors(bulider => bulider.AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin());
                
            }
        }
    }

The CustomServiceRegistraion is:

namespace KimiaBaseProject.Core.IocConfig
{
    public static class CustomServiceRegistration

    {
        
        public static void AddCustomServicesRegistery(this IServiceCollection services)
        {      
            services.AddTransient<ICourseCommandRepository, CourseCommandRepository>();
            services.AddTransient<ICourseQueryRepository, CourseQueryRepository>();
        }
    }

i have checked the application many times,but i don't understand what is the reason of it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source