I am making a project with simple login and signup functionality and I configured my Startup.cs
file which is as follows :
// 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();
}
Here I have used the method AddServiceExtensions
which is as follows :
public static IServiceCollection AddServiceExtensions(this IServiceCollection services)
{
services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserDataAccess, UserDataAccess>();
services.AddScoped<IUserDbContext, UserDbContext>();
//services.AddScoped<ExceptionHandlerFilter>();
return services;
}
I am working on .Net Core 3.1 and whenever I run the project I get the following error :
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'.
I am using Dynamodb and hence I have configured that too in the service.
Please help me resolve this issue as I have configured all the services and their interfaces in the AddServiceExtension
method.
According to the error message you are getting: you have dependencies among your services that require the right order of registration.
Try this:
public static IServiceCollection AddServiceExtensions(this IServiceCollection services)
{
services.AddScoped<IUserDataAccess, UserDataAccess>();
services.AddScoped<IUserDbContext, UserDbContext>();
services.AddScoped<IUserService, UserService>();
return services;
}
if it does not work, please edit your post and provide the constructor code of IUserService.
User contributions licensed under CC BY-SA 3.0