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

14,287

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.

Share:
14,287
Oblomingo
Author by

Oblomingo

Updated on July 01, 2022

Comments

  • Oblomingo
    Oblomingo almost 2 years

    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?

    • Morten Frederiksen
      Morten Frederiksen about 11 years
      The exception text: DocumentsController does not have a default constructor
    • Oblomingo
      Oblomingo about 11 years
      This problem solved in this article: asp.net/web-api/overview/extensibility/…
  • Jenan
    Jenan almost 9 years
    This answer resolved my problem with Unity and WebApi Controller, thanks @Oliver.