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)rn at
System.Web.Http.Internal.TypeActivator.Create[TBase](Type
instanceType)rn at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage
request, Type controllerType, Func`1& activator)rn 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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

When you install Unity for ASP.NET Web API, it does everything except add the following line to your Global.asax

Bootstrapper.Initialise();

So you need to add that to your Application_Start method:
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    Bootstrapper.Initialise();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x