3

I'm new to Web API. In my .NET Core 3.1 web api I have defined 3 constructors beleiving overloading.

[Route("api/[controller]")]
[ApiController]
public class KipController : ControllerBase
{
    private readonly IConfiguration _configuration;
    private readonly IMapper _mapper;
    private readonly dbLNePMODev1Context _context;
    public KipController(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    public KipController(IMapper mapper)
    {
        _mapper = mapper;
    }

    public KipController(dbLNePMODev1Context context)
    {
        _context = context;
    }
    ......

But when executing the application it says the following error

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: epmoAPI.Models.dbLNePMODev1Context Lifetime: Scoped ImplementationType: epmoAPI.Models.dbLNePMODev1Context': Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions1[epmoAPI.Models.dbLNePMODev1Context])) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at epmoAPI.Program.Main(String[] args) in C:\Projects\Enterprise PMO\ePMOHub\epmoAPI\Program.cs:line 16

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: epmoAPI.Models.dbLNePMODev1Context Lifetime: Scoped ImplementationType: epmoAPI.Models.dbLNePMODev1Context': Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[epmoAPI.Models.dbLNePMODev1Context])

Inner Exception 2: InvalidOperationException: Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[epmoAPI.Models.dbLNePMODev1Context])

Please help me. I am not getting an idea whats wrong in my code

2
  • why do you have 3 constructors instead of one that take 3 arguments ? Commented May 7, 2020 at 11:39
  • Sorry I am very new to DI. So honestly no idea how to deal with it. Thats why I made it 3 Commented May 7, 2020 at 11:53

4 Answers 4

1

Your IConfiguration (and probably IMapper) use dependency injection, while dbLNePMODev1Context does not. That's why ASP.NET Core can't instantiate it.

A cleaner solution:

public KipController(IConfiguration configuration = null, IMapper mapper = null, dbLNePMODev1Context context = null)
{
    _configuration = configuration;
    _mapper = mapper;
    _context = context;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. But I just tried that and its triggering System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: epmoAPI.Models.dbLNePMODev1Context Lifetime: Scoped ImplementationType: epmoAPI.Models.dbLNePMODev1Context': Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[epmoAPI.Models.dbLNePMODev1Context]))'
0

If your dbLNePMODev1Context class inherits from DbContext (If your project uses EntityFrameworkCore) then you can register dbLNePMODev1Context as a DBContext in ConfigureServices method. You can add DbContextOptionsBuilder to indicate type of DB, Connection String and other details.

public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<dbLNePMODev1Context>();
...
}

If your dbLNePMODev1Context class does not use EntityFramework then you can just register it as a service .Please decide on Scoped,Transient or Singleton. Reference

public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton(new dbLNePMODev1Context());
...
}

It would give a better idea if you share your ConfigureServices method from Startup.cs class.

Comments

0

I suppose you are using the default ASP .NET dependency injection container and you have more than one constructor in dbLNePMODev1Context. So the DI container doesn't know which constructor to use when registering the service. Try to specify it explicitly:

services.AddScoped(serviceProvider =>
 {
    var options = serviceProvider.GetRequiredService<DbContextOptions<dbLNePMODev1Context>>();
    return new dbLNePMODev1Context(options);
 });

Comments

0
[Route("api/[controller]")]
[ApiController]
public class KipController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IMapper _mapper;
private readonly dbLNePMODev1Context _context;

public KipController(IConfiguration configuration, IMapper mapper, dbLNePMODev1Context context)
    {
        _configuration = configuration;
        _mapper = mapper;
        _context = context;
    }
     // Your other action methods go here...
}

This way, you can use any combination of the dependencies in your controller actions. Make sure to update your DI configuration accordingly in your startup class to inject these dependencies correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.