'AggregateException Some services are not able to be constructed

I am getting this error :

An error occurred while starting the application. AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: IPMMS.Business.Managers.Seeding.SeedDataManager Lifetime: Scoped ImplementationType: IPMMS.Business.Managers.Seeding.SeedDataManager': Unable to resolve service for type 'IPMMS.Database.Context.IPMContext' while attempting to activate 'IPMMS.Business.Managers.Seeding.SeedDataManager'.)

I have il-ilce.json which has all cities and districts belong the cities. Onbuild, when IsSeed is true will insert into database but I got this error.

Code:

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            services.AddScoped<CustomerServices, CustomerServices>();
            services.AddScoped<SeedDataManager, SeedDataManager>(); 
        }

and also in Startup.cs

if (Convert.ToBoolean(Configuration.GetSection("IsSeed").Value)) 
{
       var seedManager = app.ApplicationServices.GetRequiredService<SeedDataManager>();
       seedManager.RunSeedData(env.WebRootPath);
}

SeedDataManager class is :

public class SeedDataManager
    {
        private readonly IPMContext _context;

        public SeedDataManager(IPMContext iPMContext)
        {
            _context = iPMContext;
        }

        public void RunSeedData(string root)
        {
            var data = File.ReadAllText(root + "seedData/il-ilce.json");
            var allData = JsonConvert.DeserializeObject<List<SeedDataDto>>(data);

            foreach (var city in allData.GroupBy(g => g.City))
            {
                var newCity = new City
                {
                    CityName = city.Key,
                    CountryId = 1,
                    Districts = city.Select(s => new District
                    {
                        DistrictName = s.District
                    }).ToList()
                };
                _context.Cities.Add(newCity);
            }
            _context.SaveChanges();
        }
    }

appsettings.json //I add only IsSeed

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "IsSeed": "true"
}


Solution 1:[1]

The problem is what the message says. SeedDataManager cannot be constructed because the DI framwork doesn't know where to take IPMContext which is required by SeedDataManager constructor.

public SeedDataManager(IPMContext iPMContext)

If IPMContext is a DbContext then you could add it in ConfigureServices like so:

 services.AddDbContext<IPMContext>(options => options.UseSqlServer(connectionString));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tymtam