Please my question may not be constructive but am still learning so kindly help.
I am designing a 3 tier architecture with dotnet core 3.1 mvc, visual studio 2019. I want to use only one connection to the database from the Data Access Layer and authenticate users using Identity from the Business logic layer. That is:
The connection string is in the data access layer and I want to configure the service through an extended IServiceCollectionExtension.
public static class IServiceCollectionExtension
{
public static IServiceCollection AddDALDependenciesLibraries(this IServiceCollection services)
{
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<ICoursesBL, CoursesBL>();
services.AddScoped<IStudentsBL, StudentBL>();
services.AddIdentity<IdentityUser, IdentityRole>).AddEntityFrameworkStores<GooddbContext>();
return services;
}
}
And the start it in the startup.cs like
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<GooddbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("EmployeeDBConnection")));
services.AddDALDependenciesLibraries();
services.AddControllersWithViews();
}
But if I start the application, it throws this error below:
System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: TestCoreApp.BLL.BusinessLogic.Interfaces.ICoursesBL Lifetime: Scoped ImplementationType: TestCoreApp.BLL.BusinessLogic.CoursesBL': Unable to resolve service for type 'AutoMapper.IMapper' while attempting to activate 'TestCoreApp.BLL.BusinessLogic.CoursesBL'.) (Error while validating the service descriptor 'ServiceType: TestCoreApp.BLL.BusinessLogic.Interfaces.IStudentsBL Lifetime: Scoped ImplementationType: TestCoreApp.BLL.BusinessLogic.StudentBL': Unable to resolve service for type 'AutoMapper.IMapper' while attempting to activate 'TestCoreApp.BLL.BusinessLogic.StudentBL'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable
1 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.ServiceFactoryAdapter
1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at TestCoreApp.UI.Program.Main(String[] args) in C:\Users\verky\source\repos\TestCoreApp\TestCoreApp.UI\Program.cs:line 16
This exception was originally thrown at this call stack:
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(System.Type, System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain, System.Reflection.ParameterInfo[], bool) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ResultCache, System.Type, System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Microsoft.Extensions.DependencyInjection.ServiceDescriptor, System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain, int) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Microsoft.Extensions.DependencyInjection.ServiceDescriptor, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(Microsoft.Extensions.DependencyInjection.ServiceDescriptor)
Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: TestCoreApp.BLL.BusinessLogic.Interfaces.ICoursesBL Lifetime: Scoped ImplementationType: TestCoreApp.BLL.BusinessLogic.CoursesBL': Unable to resolve service for type 'AutoMapper.IMapper' while attempting to activate 'TestCoreApp.BLL.BusinessLogic.CoursesBL'.
Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'AutoMapper.IMapper' while attempting to activate 'TestCoreApp.BLL.BusinessLogic.CoursesBL'.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<GooddbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("EmployeeDBConnection")));
services.AddDALDependenciesLibraries();
services.AddControllersWithViews();
}
User contributions licensed under CC BY-SA 3.0