1

InvalidOperationException: Error while validating the service descriptor 'ServiceType: BulkyBook.DataAccess.Repository.IRepository.IUnitofWork Lifetime: Scoped ImplementationType: BulkyBook.DataAccess.Repository.UnitofWork': Unable to resolve service for type 'BulkyBook.DataAccess.ApplicationDbContext' while attempting to activate 'BulkyBook.DataAccess.Repository.UnitofWork'.
InvalidOperationException: Unable to resolve service for type 'BulkyBook.DataAccess.ApplicationDbContext' while attempting to activate 'BulkyBook.DataAccess.Repository.UnitofWork'.

This Error Started when I tried to Add Identity Pages in my application by Scaffolding Items

Program.cs

using BulkyBook.DataAccess;
using BulkyBook.DataAccess.Repository;
using BulkyBook.DataAccess.Repository.IRepository;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
    builder.Configuration.GetConnectionString("DefaultConnection")
    ));

builder.Services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<IUnitofWork, UnitofWork>();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}");

app.Run();

on line var app = builder.Build I am facing this error
The code was working file before I added identity razor pages
ApplicationDbContext.cs

using BulkyBook.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace BulkyBook.DataAccess
{
    public class ApplicationDbContext: IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :base(options) 
        {
            

        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<CoverType> CoverTypes { get; set; }
        public DbSet<Product> Products { get; set; }
    }
}

UnitofWork.cs

using BulkyBook.DataAccess.Repository.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BulkyBook.DataAccess.Repository
{
    public class UnitofWork : IUnitofWork
    {
        private ApplicationDbContext _db;

        public UnitofWork(ApplicationDbContext db)
        {
            _db = db;
            Category = new CategoryRepository(_db);
            CoverType = new CoverTypeRepository(_db);
            Product = new ProductRepository(_db);
        }


        public ICategoryRepository Category { get; private set; }
        public ICoverTypeRepository CoverType { get; private set; }
        public IProductRepository Product { get; private set; }
        public void Save()
        {
            _db.SaveChanges();
        }
    }
}

This is my first WebApplication using .NET I am using .NET 6.0

I have tried referring to these questions : System.AggregateException: 'Some services are not able to be constructed' In my ASP.net core
and : https://learn.microsoft.com/en-us/answers/questions/527398/some-services-are-not-able-to-be-constructed-dbcon but not been able to resolve my error
Please do let me know if needed any more code.

Sauron
  • 66
  • 1
  • 4
  • Maybe if you post the code for the UnitOfWork class it would be helpful. – Osama Mar 28 '23 at 21:37
  • Thanks for the suggestion just added now – Sauron Mar 29 '23 at 03:35
  • Not sure what the problem is... But maybe you can try using an interface IApplicationDbContext. I have similar code but I'm using an interface. – Osama Mar 29 '23 at 10:00
  • @Osama Microsoft's [own documentation](https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#dbcontext-in-dependency-injection-for-aspnet-core) doesn't show using an interface for this to work. – mason Mar 29 '23 at 13:25
  • Thank You guys problem is now fixed. Actually there is another file ApplicationDbContext.cs got created in Areas/Identity/Data folder which were conflicting with BulkyBook.DataAccess/Data/ApplicationDbContext.cs – Sauron Mar 30 '23 at 15:51

0 Answers0