请我的问题可能没有建设性,但我仍在学习,所以请帮助。
我正在设计一个带有dotnet core 3.1 mvc,Visual Studio 2019的3层架构。我只想使用一个从数据访问层到数据库的连接,并使用业务逻辑层的标识对用户进行身份验证。那是:
连接字符串位于数据访问层中,我希望通过扩展的 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;
}
}
并在启动时启动它.cs就像
// 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();
}
但是如果我启动应用程序,它会在下面抛出此错误:
System.AggregateException HResult=0x80131500 消息=某些服务无法构造(验证服务描述符"ServiceType: TestCoreApp.BLL.BusinessLogic.Interfaces.ICoursesBL 生存期:作用域实现类型:TestCoreApp.BLL.BusinessLogic.CoursesBL"时出错:尝试激活"TestCoreApp.BLL.BusinessLogic.CoursesBL"时无法解析类型"AutoMapper.IMapper"的服务。(验证服务描述符"ServiceType: TestCoreApp.BLL.BusinessLogic.Interfaces.IStudentsBL 生存期:作用域实现类型:TestCoreApp.BLL.BusinessLogic.StudentBL"时出错:尝试激活"TestCoreApp.BLL.BusinessLogic.StudentBL"时无法解析类型"AutoMapper.IMapper"的服务。 Source=Microsoft.Extensions.DependencyInjection 堆栈跟踪: 在 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
此异常最初在此调用堆栈中引发:
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(
内部异常 1:
InvalidOperationException:验证服务描述符"ServiceType: TestCoreApp.BLL.BusinessLogic.Interfaces.ICoursesBL Lifetime: Scoped ImplementationType: TestCoreApp.BLL.BusinessLogic.CoursesBL":尝试激活"TestCoreApp.BLL.BusinessLogic.CoursesBL"时无法解析类型"AutoMapper.IMapper"的服务。
内部异常 2:
InvalidOperationException:尝试激活"TestCoreApp.BLL.BusinessLogic.CoursesBL"时无法解析类型"AutoMapper.IMapper"的服务。
//此方法由运行时调用。使用此方法将服务添加到容器。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<GooddbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("EmployeeDBConnection")));
services.AddDALDependenciesLibraries();
services.AddControllersWithViews();
}