Unity 5 Dependency Injection in ASP.NET MVC 5

In ASP.NET Core, dependency injection is built-in which makes things pretty easy. In MVC 5, you need to install one of the many dependency injection packages. Unfortunately, sometimes the documentation is incomplete and tutorials on the web are broken. I actually ran into at least two tutorials that weren't working for me. For instance, this article points to installing Unity 4.0.1 but unfortunately that one does not install the UnityConfig.cs file that it claims nor does it install one of Unity.MVC# versions that is required for the call to UnityDependencyResolver.

Getting Unity to work in MVC is pretty easy though if you have the right package combination. Running the following command in package manager console will install Unity.MVC5 version 1.3.0 and Unity 5.2.0

Install-package unity.mvc5 -version 1.3.0

This package will install the UnityConfig.cs file which is easy to configure. Once you have that set up, all you need to do is to add a call to the RegisterComponents method in that class in the Application_Start section of your global.asax.cs file


That should do it.

Visual Studio
Microsoft IIS
SQL Server
Windows Script Host
MS DNS Server
Microsoft FTP
Filezilla Server
Visual Basic

Tel: 720-383-4246
Tel: 678-739-9423