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:
- register interface-to-concrete mappings in the Unity container
- set Web API's dependency resolver to a Unity-based resolver
If either step is missing, this controller will fail to resolve:
Web API needs a container-aware resolver to know how to create IOrderService.
Minimal Unity Setup
And call it during application startup:
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:
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
UnityDependencyResolvertoGlobalConfiguration. - 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.

