0
votes

I am a bit new to DI pattern and having some problems in hand. The whole story goes like this:

  • There is a console app called 'DataGenerator' responsible to generate sample data for the entire app. A reference to 'Autofac' has been added to this project. Here is the code snippet for creating a list of country:

enter image description here

The exception raising up in the above foreach loop says:

{"None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'TechnicalTest.Service.AppServices.CountryService' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'TechnicalTest.Repository.EntityRepositories.ICountryRepository countryRepository' of constructor 'Void .ctor(TechnicalTest.Repository.EntityRepositories.ICountryRepository, TechnicalTest.Repository.UnitOfWork.IUnitOfWork)'."}

  • There is another project including the app services. Here is one of which:

enter image description here

Please note that a GenericRepository and UnitOfWork project has been added to introduce every entity repository. For example: enter image description here

I have search through the web for the exception and how I could get ride of it but unfortunately could not figure out a solution for my case. I guess there must be something wrong with the CountryService constructor or something like that. Any helps or ideas would be kindly appreciated.

1
Have you registered DbContext with your container? From the error message it looks like the problem is with resolving the dependencies for CountryRepository that is the problem. - Jack Hughes
@JackHughes: am sorry the code of the main class I have posted missed a line of code builder.RegisterType<CountryService>().As<ICountryService>();. Isn't it enough to use the service or DbContext should be registered too? - Ali
All dependencies for classes registered in the container must be registered with the container. How can the container create an instance of CountryRepository without all of the dependencies it requires? - Jack Hughes
..well it seems you're right... I should do that and see what happens - Ali
@JackHughes: Thanks a million.. now it works fine and am so happy with that.. Just one more question: to register the DbContext I've made a reference to my Model project in which DbContext and DbSet<T> have been written. Do you think this referencing would be fine in terms of design and architecture? I guess it sounds not bad as I don't know any other course of action.. - Ali

1 Answers

2
votes

Have you registered DbContext with your container? From the error message it looks like the problem is with resolving the dependencies for CountryRepository.

All dependencies for classes registered in the container must be registered with the container. How can the container create an instance of CountryRepository without all of the dependencies it requires?