Dependency Injection
ASP.NET
Web API
Unity Container
Controller Injection

Cannot Inject Dependencies into ASP.NET Web API Controller using Unity

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If constructor injection does not work in an ASP.NET Web API controller when using Unity, the usual problem is not the controller class itself. The problem is that Web API is still creating controllers with its default activator because Unity was never properly registered as the dependency resolver.

For legacy ASP.NET Web API, Unity must be connected explicitly to GlobalConfiguration. If that resolver hook is missing or registrations are incomplete, controller creation falls back to the default pipeline and constructor injection fails.

What Must Be Wired Up

There are two essential steps:

  1. register interface-to-concrete mappings in the Unity container
  2. set Web API's dependency resolver to a Unity-based resolver

If either step is missing, this controller will fail to resolve:

csharp
1public class OrdersController : ApiController
2{
3    private readonly IOrderService _service;
4
5    public OrdersController(IOrderService service)
6    {
7        _service = service;
8    }
9}

Web API needs a container-aware resolver to know how to create IOrderService.

Minimal Unity Setup

csharp
1using System.Web.Http;
2using Unity;
3using Unity.Lifetime;
4using Unity.WebApi;
5
6public static class UnityConfig
7{
8    public static void RegisterComponents()
9    {
10        var container = new UnityContainer();
11
12        container.RegisterType<IOrderService, OrderService>(new HierarchicalLifetimeManager());
13
14        GlobalConfiguration.Configuration.DependencyResolver =
15            new UnityDependencyResolver(container);
16    }
17}

And call it during application startup:

csharp
1protected void Application_Start()
2{
3    GlobalConfiguration.Configure(WebApiConfig.Register);
4    UnityConfig.RegisterComponents();
5}

Without that last assignment to DependencyResolver, Unity is not actually in the controller creation path.

Typical Failure Modes

Common reasons injection still fails:

  • the resolver was never assigned
  • the registration method never ran
  • the interface mapping is missing
  • the wrong lifetime manager causes disposal issues
  • MVC and Web API dependency configuration were mixed up

That last point matters because classic ASP.NET MVC and ASP.NET Web API are related but not identical pipelines. Registering Unity for MVC does not automatically configure Web API controller activation.

How To Verify the Container

A simple way to sanity-check the configuration is to resolve the service directly:

csharp
1var container = new UnityContainer();
2container.RegisterType<IOrderService, OrderService>();
3
4var service = container.Resolve<IOrderService>();
5Console.WriteLine(service.GetType().Name);

If direct resolution works but controller injection does not, the missing piece is usually the Web API dependency resolver hookup.

Lifetime Considerations

Choosing the wrong lifetime can also create confusing behavior. Stateless services often work fine with transient registration. Shared services, repositories, or per-request resources may need more careful lifetime control.

The key is consistency. A working resolver with broken lifetime management can look like a controller-injection problem when it is really an object-lifecycle problem.

Constructor Design Still Matters

Even with Unity configured correctly, controller constructors should stay focused on real dependencies. If a controller takes too many services, debugging resolution failures becomes harder and the controller likely has too many responsibilities. Good DI wiring and good controller design reinforce each other.

Common Pitfalls

  • Registering Unity types but never assigning GlobalConfiguration.Configuration.DependencyResolver.
  • Configuring MVC dependency resolution and assuming Web API will use it automatically.
  • Forgetting to register one of the interfaces required by the controller constructor.
  • Using the wrong Unity package or resolver implementation for Web API.
  • Debugging the controller class instead of verifying the application startup wiring first.

Summary

  • Unity injection into Web API controllers works only if Web API is told to use Unity as its resolver.
  • Register services in the container and assign UnityDependencyResolver to GlobalConfiguration.
  • Make sure the registration code actually runs during application startup.
  • Distinguish MVC DI wiring from Web API DI wiring in legacy ASP.NET apps.
  • If direct container resolution works but controller injection does not, the resolver hookup is the first thing to inspect.

Course illustration
Course illustration

All Rights Reserved.