Why my Application keep calling the Class Constructor which has service Provider Parameter

42 Views Asked by At

I'm having issues with Constructor called by default

below is my class

public class Repo : BaseRepo
{        
    public Repo :base() { }

    // Added Below Constructor for Unit Testing Purpose to pass Mock Service 
    // Provider to base Repo before it instantiates Other Services. 

    public Repo(IServiceProvider serviceProvider) : base(serviceProvider) { }
}    

When even I run my application outside of unit test project, the Constructor with service Provider Parameter is getting Called, is there a way to run this constructor only for unit test project.

I tried added symbols, but it did not work

1

There are 1 best solutions below

0
pakeha_by On

By default DI is choosing constructor with maximum number of parameters it can resolve.

Try to apply attribute [Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute] to the parameterless constructor, DI should pick it up instead.