I'm writing the web api using Dependency injection, Unit of work with the help of repositories and Autofac as the container. Dependency was getting injected perfectly 24 hours ago but suddenly when i started working today i kept on receiving the error
"Message": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'SearchController'. Make sure that the controller has a parameterless public constructor.", "ExceptionType": "System.InvalidOperationException",
I will include my signatures and how i am registering the types and will be really glad if someone can point out what might be going wrong with my code.
On my web api controller, i have
private IUnitOfWork<Listing> _unitOfWork = null;
public SearchController(IUnitOfWork<Listing> unitOfWork)
{
_unitOfWork = unitOfWork;
}
Unit of work takes the generic type parameter to create repository.
In my WebApiConfig.cs, I'm registering the types as 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'm registering the SearchController to use the constructor which takes in IUnitOfWork<>. It was all working well before i added Mocked unit tests but for some purpose i keep on getting this error now. I've also registered the DependencyResolver
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
config.DependencyResolver = resolver;
As there are quite a few upvotes on this question without any answers yet and I don't exactly remember how I made it work but I want to share the latest technique that has been working perfectly in many projects.
1) This is how I register my generic repsoitory and unit of work
builder.RegisterType(typeof(YourContextInRepository));
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>));
2) This is how I setup the dependency resolver 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 on MVC as well as WebAPI controllers and I can simply do
private IUnitOfWork<Listing> _uow;
public SearchController(IUnitOfWork<Listing> uow)
{
_uow = uow;
}
Hope it helps someone in the future.
I faced with the same issue and the problem was that I were using Autofac code samples for ContainerBuilder() object for MVC controllers and not API.
Here is a good sample how to configure it with ApiController
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With