My ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi. In the same project simple controllers works with Unity.Mvc3 properly. But when I run Web API controller derived from ApiController I'm getting a message:
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Type 'ElectricTests.Controllers.Api.DocumentsController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}
My ApiController:
public class DocumentsController : ApiController
{
private readonly IDocumentsRepository _repository;
public DocumentsController(IDocumentsRepository repository) {
_repository = repository;
}
public IEnumerable<FormattedDocument> GetFormattedDocuments()
{
return _repository.GetAllFormattedDocuments();
}
...
Bootstrapper.cs:
public static class Bootstrapper {
public static void Initialise() {
IUnityContainer container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer() {
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IDocumentsRepository, DocumentsRepository>();
container.RegisterType<IQuestionsRepository, QuestionsRepository>();
container.RegisterType<ITestRepository, TestsRepository>();
return container;
}
}
Where is my mistake?