1

I have an ApiController in an Area of my MVC website, I'm injecting dependencies into it via Unity and my controllers extend the System.Web.Http.ApiController.

I'm using Unity from Microsoft.Practices.Unity.dll, v3.0.0.0.

I can route to the controller with the following in ApiAreaRegistration

context.MapRoute(
    "Api_default",
    "Api/users/{action}/{id}"
);

But I get the following error :

Type 'Project.Areas.Api.Controllers.UsersController' does not have a default constructor

However if I add a default constructor my dependencies don't get resolved.

I'm beginning to feel like I'm missing something structural?

3
  • 1
    MapRoute isn't related to your Unity problem. Is your controller extending System.Web.Http.ApiController? Are you using the Unity.WebApi package? Commented Aug 27, 2013 at 0:18
  • @Jasen, Thanks I've added the details to my original question. Commented Aug 27, 2013 at 10:06
  • You do realize that there are separate dependency resolver registrations for WebApi, compared to MVC, right? Commented Sep 2, 2013 at 21:54

2 Answers 2

3

You don't show your controller registration and the error message suggests you have not registered the controller dependencies.

I use the Unity.WebAPI NuGet package to take care of the controller build-up and container lifetime management. If your project also uses MVC controllers the Unity.Mvc3 will handle those controllers. These packages get Unity wired-up for my controllers with very little code.

My Unity bootstrap looks like this

public static class UnityConfig
{
    var container = BuildUnityContainer();

    // MVC controllers
    DependencyResolver.SetResolver(new Unity.Mvc3.UnityDependencyResolver(container));
    // WebAPI controllers
    GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
}

private static IUnityContainer BuildUnityContainer()
{
    var container = new UnityContainer();

    // register all your components with the container here
    // you don't need to register controllers
    container.RegisterType<IUsersService, UsersService>();
    ...
    return container;
}

And I don't worry about my controller creation -- It just works.

public class UsersController : ApiController
{
    private IUsersService service;

    public UsersController(IUsersService service)
    {
        this.service = service;
    }

    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2
    GlobalConfiguration.Configuration.DependencyResolver =
        new UnityDependencyResolver(YOUR_UNITY_CONTAINER);

and from http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx#sec18 you can find that you should search for 'Unity bootstrapper for ASP.NET WebApi' and you get the UnityDependencyResolver from the namespace: 'Microsoft.Practices.Unity.WebApi'.

Easiest way is just to install the WebApi Bootstrapper and look in the App_Start folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.