【问题标题】:HResult=0x80131500 Message=Some services are not able to be constructed ASP.NET CoreHResult=0x80131500 Message=某些服务无法构建 ASP.NET Core
【发布时间】:2020-12-27 13:57:22
【问题描述】:

我正在制作一个具有简单登录和注册功能的项目,我配置了我的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:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.API\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.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 方法中配置了所有服务及其接口。

【问题讨论】:

  • 您可以编辑您的帖子并添加 IUserService 和 UserService 类代码吗?
  • 能否提供UserService、UserDbContext和UserDataAccess的实现?

标签: c# asp.net-core amazon-dynamodb


【解决方案1】:

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

试试这个:

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

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

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2023-02-03
    • 2021-12-12
    • 2021-07-07
    • 2020-11-19
    • 2021-03-14
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多