HResult=0x80131500消息=某些服务无法构造ASP.NET核心



我正在制作一个具有简单登录和注册功能的项目,并配置了我的Startup.cs文件,如下所示:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
/* Add Services */
services.AddServiceExtensions();
/* Add controllers */
services.AddControllers();
/* Cors Extension */
services.AddCorsExtension();
/* Swagger Setup */
services.AddSwaggerDocumentation();
/* Add Mvc Extensions */
services.AddMvc(option => option.EnableEndpointRouting = false);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddHttpContextAccessor();
services.AddDbContext<UserDbContext>();
/* Dynamo Db Setup */
var dynamoDbConfig = Configuration.GetSection("DynamoDb");
var runLocalDynamoDb = dynamoDbConfig.GetValue<bool>("LocalMode");
services.AddSingleton<IAmazonDynamoDB>(sp =>
{
var clientConfig = new AmazonDynamoDBConfig
{
ServiceURL = dynamoDbConfig.GetValue<string>("LocalServiceUrl")
};
return new AmazonDynamoDBClient(clientConfig);
});
/* AutoMapper Configuration */
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(AutoMapperProfileConfiguration).Assembly);
});
services.AddMvcCore();
}

在这里,我使用了方法AddServiceExtensions,如下所示:

public static IServiceCollection AddServiceExtensions(this IServiceCollection services)
{
services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserDataAccess, UserDataAccess>();
services.AddScoped<IUserDbContext, UserDbContext>();
//services.AddScoped<ExceptionHandlerFilter>();
return services;
}

我正在开发.Net Core 3.1,每当我运行该项目时,我都会收到以下错误:

System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: NeighborhoodHelpers.UserMicroservice.Services.UserServices.IUserService Lifetime: Scoped ImplementationType: NeighborhoodHelpers.UserMicroservice.Services.UserServices.UserService': Unable to resolve service for type 'NeighborhoodHelpers.UserMicroservice.Entities.DatabaseContext.UserDbContext' while attempting to activate 'NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess'.) (Error while validating the service descriptor 'ServiceType: NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.IUserDataAccess Lifetime: Scoped ImplementationType: NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess': Unable to resolve service for type 'NeighborhoodHelpers.UserMicroservice.Entities.DatabaseContext.UserDbContext' while attempting to activate 'NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, IServiceProviderEngine engine, 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 NeighborhoodHelpers.UserMicroservice.API.Program.Main(String[] args) in D:GursimranHackathon2NeighborhoodHelpers.UserMicroservice.APINeighborhoodHelpers.UserMicroservice.APIProgram.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.TryCreateExact(System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite.AnonymousMethod__0(System.Type)
System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey, System.Func<TKey, TValue>)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(System.Type, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain)
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)
...
[Call Stack Truncated]
Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: NeighborhoodHelpers.UserMicroservice.Services.UserServices.IUserService Lifetime: Scoped ImplementationType: NeighborhoodHelpers.UserMicroservice.Services.UserServices.UserService': Unable to resolve service for type 'NeighborhoodHelpers.UserMicroservice.Entities.DatabaseContext.UserDbContext' while attempting to activate 'NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess'.
Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'NeighborhoodHelpers.UserMicroservice.Entities.DatabaseContext.UserDbContext' while attempting to activate 'NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess'.

我正在使用Dynamodb,因此我也在服务中配置了它。请帮助我解决这个问题,因为我已经在AddServiceExtension方法中配置了所有服务及其接口。

根据您收到的错误消息:您的服务之间存在依赖关系,需要正确的注册顺序。

试试这个:

public static IServiceCollection AddServiceExtensions(this IServiceCollection services)
{
services.AddScoped<IUserDataAccess, UserDataAccess>();
services.AddScoped<IUserDbContext, UserDbContext>();
services.AddScoped<IUserService, UserService>();
return services;
}

如果不起作用,请编辑您的帖子并提供IUserService的构造函数代码。

最新更新