'Some services are not able to be constructed' Blazor server app

I am trying to write a simple Blazor Server app (which I have done before) and I am getting a ‘System.AggregateException’ error when the app tries to build.

Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: CrispyAcers.Services.IFarmService Lifetime: Transient ImplementationType: CrispyAcers.Services.FarmService’: Unable to resolve service for type ‘Microsoft.EntityFrameworkCore.IDbContextFactory`1[CrispyAcers.Models.FarmContext]’ while attempting to activate ‘CrispyAcers.Services.FarmService’.)

This is my Program.cs

var builder = WebApplication.CreateBuilder(args);


// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

builder.Services.AddDbContext<FarmContext>(option =>
    option.UseSqlServer(builder.Configuration.GetConnectionString("FarmConnection")));

builder.Services.AddTransient<IFarmService, FarmService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/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.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

This is the FarmService I am trying to add

public class FarmService : IFarmService
    {
        private readonly IDbContextFactory<FarmContext> dbFactory;

        public FarmService(IDbContextFactory<FarmContext> context)
        {
            dbFactory = context;
        }

        public List<Animals> GetAnimalsList()
        {
            using (var context = dbFactory.CreateDbContext())
            {
                return context.Animals.ToList();
            }
        }
    }```


I could very easily be missing something simple which hopefully someone will catch. If I remove the FarmService the app builds fine. I have tried Scoped, Trasient, and Singleton lifetimes but nothing different happens.

>Solution :

For IDbContextFactory<FarmContext> service

You need to register the service of IDbContextFactory<FarmContext> type.

builder.Services.AddDbContextFactory<FarmContext>(opt =>
    option.UseSqlServer(builder.Configuration.GetConnectionString("FarmConnection")));

For IDbContext<FarmContext> service

Alternatively, you should use the IDbContext<FarmContext> service instead of IDbContextFactory<FarmContext>. Modify the FarmService as below:

public class FarmService : IFarmService
{
    private readonly IDbContext<FarmContext> _context;

    public FarmService(IDbContext<FarmContext> context)
    {
        _context = context;
    }

    public List<Animals> GetAnimalsList()
    {
        return _context.Animals.ToList();
    }
}

Reference

Database access – ASP.NET Core Blazor Server with Entity Framework Core (EF Core)

Leave a Reply