Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi

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?

like image 743
Oblomingo Avatar asked Apr 21 '13 10:04

Oblomingo


1 Answers

The handling of Controller and ApiController is different as they have completely different base classes:

I use Unity.MVC4 library for controller DI (http://www.nuget.org/packages/Unity.MVC4/)

Install-Package Unity.MVC4

and Unity.WebAPI for DI (http://www.nuget.org/packages/Unity.WebAPI/)

Install-Package Unity.WebAPI

Your bootstrapper should be a combination of both:

DependencyResolver.SetResolver(new Unity.Mvc4.UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

Note I also had to do to add some registration to get the Help page to work

container.RegisterInstance(typeof (HttpConfiguration), GlobalConfiguration.Configuration);

As the owner of Unity.MVC4 I am looking at getting WebApi implemented within our library.

like image 66
Oliver Avatar answered Sep 30 '22 11:09

Oliver