Naruto Reddy

Naruto Reddy

  • NA
  • 123
  • 38.9k

Error: "Some services are not able to be constructed"

Jun 15 2020 3:03 AM
I am getting this error can anyone help me ?
 
Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: LServices.Interfaces.IUpdater Lifetime: Scoped ImplementationType: LServices.IUpdater ': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate 'LServices.Repositories.Base.Repository`1[LServices.Entities.Students]'.
Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate 'LServices.Repositories.Base.Repository`1[LServices.Entities.Students]]'.
 
I have my files as below :
Startup.cs
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3. services.AddDbContext(item =>  
  4. item.UseSqlServer(Configuration.GetConnectionString("DBConnection")));  
  5. services.AddTransient();  
  6. services.AddTransient(typeof(IRepository<>), typeof(Repository<>));  
  7. services.AddTransient(typeof(IGenericRepository<,>), typeof(GeneralRepository<,>));  
  8. }  
  9. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  10. {  
  11. if (env.IsDevelopment())  
  12. {  
  13. app.UseDeveloperExceptionPage();  
  14. // var appl = app.ApplicationServices.GetServices();  
  15. var appl2 = app.ApplicationServices.GetService();  
  16. appl2.GetFloorsDataInDb();  
  17. app.UseRouting();  
  18. app.UseEndpoints(endpoints =>  
  19. {  
  20. endpoints.MapGet("/", async context =>  
  21. {  
  22. await context.Response.WriteAsync("Hello World!");  
  23. });  
  24. });  
  25. }  
Context class :
  1. public class LServiceContext : DbContext  
  2. {  
  3. public LServiceContext (DbContextOptions options) : base(options)  
  4. {  
  5. }  
  6. public DbSet<Student> Student{ getset; }  
  7. }  
The class which I want to class in startup.cs
  1. namespace LServiceJob  
  2. {  
  3. public classUpdater : IUpdater  
  4. {  
  5. private IRepository studentsRepository;  
  6. public FloorsInfoUpdater(IRepository studentRepository)  
  7. {  
  8. this.studentRepository= studentRepository;  
  9. }  
  10. public List GetFloorsDataInDb()  
  11. {  
  12. List groupedFloors = this.studentRepository.GetAll().ToList();  
  13. return groupedstuents;  
  14. }  
  15. }  
  16. }  

Answers (2)