WebApi DI Autofac - make sure the controller has an immortal public constructor

I am writing web api using Dependency injection, Unit of work using repositories and Autofac as a container. The addiction was quickly introduced 24 hours ago, but suddenly, when I started working today, I kept getting an error

"Message": "An error occurred.", "ExceptionMessage": "An error occurred while trying to create a controller of type 'SearchController. Make sure the controller has no public constructor.", "ExceptionType": "System.InvalidOperationException",

I will include my signatures and how I register types, and will be very happy if someone can indicate what might be wrong with my code.

On my api web controller i have

private IUnitOfWork<Listing> _unitOfWork = null; public SearchController(IUnitOfWork<Listing> unitOfWork) { _unitOfWork = unitOfWork; } 

The unit of work accepts a generic type parameter to create the repository.

In my WebApiConfig.cs, I register the types below

  builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)); builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>)).InstancePerDependency(); builder.RegisterType(typeof(SearchController)).UsingConstructor(typeof(IUnitOfWork<Listing>)); 

I am registering a SearchController to use a constructor that accepts IUnitOfWork <>. All of this worked well before I added the Mocked unit tests, but for some purposes I keep getting this error. I also registered DependencyResolver

 var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = resolver; 
+8
c # dependency-injection unit-of-work autofac asp.net-web-api2
source share
2 answers

Since there are quite a few answers to this question, but I don’t remember exactly how I did it, but I want to share the latest technology that works great in many projects.

1) This is how I register my shared repository and work block

  builder.RegisterType(typeof(YourContextInRepository)); builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)); builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>)); 

2) This is how I configure the dependency converter for my WebAPI

 // Set the dependency resolver for Web API. var webApiResolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = webApiResolver; 

3) To register it for MVC controllers,

 // Set MVC DI resolver to use our Autofac container DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

This approach works with both MVC and WebAPI controllers, and I can just do

 private IUnitOfWork<Listing> _uow; public SearchController(IUnitOfWork<Listing> uow) { _uow = uow; } 

Hope this helps someone in the future.

+6
source share

I ran into the same problem and the problem was that I used Autofac code samples for the ContainerBuilder () object for MVC controllers, not the API.

Here is a good example of how to configure it using ApiController

+1
source share

All Articles