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;