0

I am creating a project with ASP.NET Core 3.1 and was writing the BusinessManager layers to connect to database. It was going without a hitch but I come to roadblock when I added the service for the blog table for the blogs I will add.

 services.AddScoped<IBlogBusinessManager, BlogBusinessManager>();

This is how I am trying to add the service. But while I starting the project this error appears;

AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MyBlog.BusinessManagers.Interfaces.IBlogBusinessManager Lifetime: Scoped ImplementationType: MyBlog.BusinessManagers.BlogBusinessManager': Unable to resolve service for type 'MyBlog.Service.Interfaces.IBlogService' while attempting to activate 'MyBlog.BusinessManagers.BlogBusinessManager'.)

Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)

This is the BlogService class mentioned in the error:

public class BlogService : IBlogService
{
    private readonly ApplicationDbContext _applicationDbContext;

    public BlogService(ApplicationDbContext applicationDbContext)
    {
        this._applicationDbContext = applicationDbContext;
    }

    public async Task<Blog> Add(Blog blog)
    {
        _applicationDbContext.Add(blog);
        await _applicationDbContext.SaveChangesAsync();
        return blog;
    }
}

And this is the interface I use

public interface IBlogService
{
    Task<Blog> Add(Blog blog);
}

Is there any solution for this error?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Your class doesn't have an empty ctor. You'd have to set up DI so it knows how to instantiate ApplicationDbContext [see this question](https://stackoverflow.com/questions/41058142/injecting-dbcontext-into-service-layer) – Nikki9696 May 14 '21 at 20:43
  • 2
    According to the exception, the IOC container can´t resolve IBlogService while activating BlogBussinessManager. Did you register an implementation of IBlogService in your service collection? – Jota.Toledo May 14 '21 at 21:16
  • Can you show controller injected IBlogService? – tedd May 15 '21 at 04:41
  • You need to show your `ConfigureServices` method in `Startup.cs`. – Andy May 15 '21 at 05:43
  • Can you post the relates code about the `IBlogBusinessManager` and `BlogBusinessManager`? Besides, in the `Startup.ConfigureServices` method, please check your code and make sure you have registered the `IBlog` service, code like this: `services.AddScoped();`. And if you are using `IBlogService` service in the IBlogBusinessManager service, the IBlogService service should be registered before the IBlogBusinessManager service: `services.AddScoped(); services.AddScoped();` – Zhi Lv May 17 '21 at 05:54

0 Answers0