Using Unity in ASP.NET MVC 4
The following command in the Package Manager console will install Unity.Mvc3 package into your ASP.NET MVC 4 WebAPI application.
PM > Install-Package Unity.Mvc3
Unity.Mvc3 is a package that allows simple Integration of Microsoft's Unity IoC container with ASP.NET MVC 3. It also works great for ASP.NET MVC 4.
After installation of the package you will find the Bootstrapper.cs file in the root of your ASP.NET MVC 4 WebAPI project. The course code of that file is:
public static class Bootstrapper { public static void Initialise() { var container = BuildUnityContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); // register all your components with the container here // e.g. container.RegisterType<itestservice, testservice="">(); return container; } }
We will return to this class a little bit later.
To get started, we should add a call to Bootstrapper.Initialize() in the end of the Application_Start method of Global.asax.cs and the MVC framework will then use the Unity.Mvc3 DependencyResolver to resolve your components.
protected void Application_Start() { // Default code of the method // . . . // Initialization of our Unity container Bootstrapper.Initialise(); }
Using Unity in ASP.NET MVC 4 WebAPI
Unfortunately Unity.Mvc3 doesn't change the DependencyResolver for WebAPI. I hope it will be implemented in Unity.Mvc4.
However, there is the nuget package that has already implemented needed functionality - Unity.WebAPI.
The following command in the Package Manager console will install Unity.WebAPI package into your ASP.NET MVC 4 WebAPI application.
PM > Install-Package Unity.WebAPI
Unity.WebAPI is a library that allows simple Integration of Microsoft's Unity IoC container with ASP.NET's WebAPI. This project includes a bespoke DependencyResolver that creates a child container per HTTP request and disposes of all registered IDisposable instances at the end of the request.
After installation of the package you just have to add one line of code to your Bootstrapper class into the Initialise method:
public static void Initialise() { var container = BuildUnityContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); // register dependency resolver for WebAPI RC GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); // if you still use the beta version - change above line to: //GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new Unity.WebApi.UnityDependencyResolver(container)); }
After that you are ready to create your business logic.
Create service
Let's start with the service class that we want to use in our containers.
First, we will create the interface:
public interface IValuesService { int Create(string value); string Update(int id, string value); void Delete(int id); IDictionary<int, string> GetAll(); string Get(int id); }
And the service class which contains realization of this interface:
public class ValuesService : IValuesService { private Dictionary<int, string> _values; public ValuesService() { _values = new Dictionary<int, string> { {1, "Value 1"}, {2, "Value 2"}, {3, "Value 3"}, {4, "Value 4"}, {5, "Value 5"}, }; } private int GetNextId() { return _values.Keys.Max() + 1; } public int Create(string value) { var id = GetNextId(); _values.Add(id, value); return id; } public string Update(int id, string value) { _values[id] = value; return Get(id); } public void Delete(int id) { _values.Remove(id); } public IDictionary<int, string> GetAll() { return _values; } public string Get(int id) { return _values[id]; } }
This is the really simple service with some predefined values in the constructor (just for this simple example). Of course in the real life your services will be much more complex.
Register service
The next step is register our service and interface in Unity container. This is very simple, you just have to add one line of code to the Bootstrapper class into the BuildUnityContainer method:
private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); // register values service container.RegisterType<IValuesService, ValuesService>(); return container; }
As you can see it is really simple.
Also, if you read this method carefully you should see that Unity.Mvc3 registered all of our controllers by default in the Unity container. This is the line where it's happened:
container.RegisterControllers();
Inject service to Controller
So, let's get our service in the home controller:
public class HomeController : Controller { private readonly IValuesService _valuesService; public HomeController(IValuesService valuesService) { _valuesService = valuesService; } public ActionResult Values() { var viewModel = _valuesService.GetAll(); return View(viewModel); } // old code public ActionResult Index() { return View(); } }
I've just added the constructor which receive IValuesService as a parameters. And Unity will do all the "magic" of resolving out service interface for us.
Then. to test it, let's create a simple view named as the same as the action - Values.cshtml (tip: you can create a view just clicking the right mouse button on the action name, and selecting 'Add View...' in the context menu):
@model Dictionary<int,string> @{ ViewBag.Title = "Values"; } <h2>Values</h2> <ul> @foreach(var item in Model) { <li>ID: @item.Key, Value: @item.Value</li> } </ul>
Then, compile our solution and run it.
Then, navigate to http://localhost:<your port number>/Home/Values. You should see the list of the five values with ID.
If you see it then injection for our Controllers works properly.
Inject service to ApiController
Next, let's inject our service to ApiControllers:
public class ValuesController : ApiController { private readonly IValuesService _valuesService; public ValuesController(IValuesService valuesService) { _valuesService = valuesService; } // GET /api/values public IEnumerable<KeyValuePair<int, string>> Get() { return _valuesService.GetAll(); } // GET /api/values/5 public string Get(int id) { return _valuesService.Get(id); } // POST /api/values public void Post(string value) { _valuesService.Create(value); } // PUT /api/values/5 public void Put(int id, string value) { _valuesService.Update(id, value); } // DELETE /api/values/5 public void Delete(int id) { _valuesService.Delete(id); } }
To test is everything fine with the ApiController, just compile the solution and run it.
After that, open the IE developers tools and click the 'Start capturing' button in the Network tab.
Then, navigate to http://localhost:<your port number>/api/values.
You should see something like this in the Response body tab:
[{"key":1,"value":"Value 1"},{"key":2,"value":"Value 2"},{"key":3,"value":"Value 3"},{"key":4,"value":"Value 4"},{"key":5,"value":"Value 5"}]
So, for now all of your Controllers and ApiControllers can use your services.
That's all.
UPDATED:
You can download sources at: UnityApplication14.zip
I'm so happy that I've found this blog!
ReplyDeleteThanks for the good words! :)
DeleteI'll have to second that. This post saved me from having to figure this out and spending some time on it. going to use that time to check out the other posts
ReplyDeleteI'm happy about that. There are not many posts yet, but i'm working on it.
DeleteGreat Post! Thx!
ReplyDeleteThank you!
DeleteAndrew: When I get to the line that says to add this to the bootstrapper "GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new Unity.WebApi.UnityDependencyResolver(container));", there is no ServiceResolver property available on Configuration. Is there something I'm missing?
ReplyDeletethanks,
Colin
Hello Colin, that because I wrote this article for ASP.NET MVC 4 Beta, but you use ASP.NET MVC 4 RC. In that case you have to change this line to:
DeleteGlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
I've changed the code in the article
DeleteThis is nice and informative post, Thanks to post.
ReplyDeleteAsp.net Development
Great!
ReplyDeleteThis comment has been removed by the author.
ReplyDeletewhy is the service IValueService injected into constructor of controller? Why not just access a ServiceLocator from the controller methods?
ReplyDeletepublic IEnumerable> Get()
{
return ServiceLocatorOfSomeKind().GetAll();
}
Well, it depends. I use the rule: If the class cannot do its job w/o the dependency, then add it to the constructor. But if the class can work w/o the dependency, a setter is fine.
DeleteAlso good overview about injection made by Martin Fowler:
http://martinfowler.com/articles/injection.html#ConstructorVersusSetterInjection
On debug i get the following exception:
ReplyDelete{"The type Unity.WebApi.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.\r\nParameter name: commonServiceLocator"}
Also : Microsoft.Practices.Unity.UnityContainer' does not contain a definition for 'RegisterControllers' and no extension method 'RegisterControllers' accepting a first argument of type 'Microsoft.Practices.Unity.UnityContainer' could be found (are you missing a using directive or an assembly reference?)
It would be very helpful if you publish a working project for downloading and studying.
I think you have to reinstall the Unity.WebAPI nuget package. And be sure that you use ASP.NET MVC 4.0 RC, if you still use the beta version I suggest to move to RC ASAP.
DeleteFor now you should use: Unity v2.1.505.2, Unity.WebAPI v0.10, Unity.Mvc3 v1.2, ASP.NET MVC 4 (RC) v4.0.20505.0, ASP.NET Web API (RC) v4.0.20505.0
DeleteAlso, RegisterControllers() method doesn't needed anymore. I've updated the post. Thanks.
DeleteI'm working with the version of MVC4 that is installed with VS2012 RTW. Just a plain old mvc4 site, no webapi stuff yet. Anyway, I'm getting the message that "The type Unity.Mvc3.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator." when I call "DependencyResolver.SetResolver(New UnityDependencyResolver(container))". I've tried uninstalling and reinstalling Unity.MVC3. Didn't help. Any ideas?
DeleteI think you have to update your MVC libraries. The latest version is 4.0.20710.0. You can do it easily by RMB click on 'References' of your project and then click 'Manage NuGet Packages...'. Then select 'Updates' on the left panel and update ASP.NET MVC 4 to the latest version. Do it the same for ASP.NET Web API.
DeleteI've updated all my libraries as mentioned and i'm still getting the missing IserviceLocator implementation exception while running this line :
DeleteDependencyResolver.SetResolver(new UnityDependencyResolver(container));
any ideas?
I've included the sources to this topic. Enjoy.
DeleteThe UnityDependencyResolver included in Unity.WebApi does not appear to inherit from IServiceLocator (hence the rather annoying explosion). All you need to do is to wrap this class with the interface. I found that this works a treat:
Deletepublic class CustomUnityDependencyResolver : UnityDependencyResolver, IServiceLocator
{
public CustomUnityDependencyResolver(IUnityContainer container) : base(container) {}
public object GetInstance(Type serviceType)
{
return Container.Resolve(serviceType);
}
public object GetInstance(Type serviceType, string key)
{
return Container.Resolve(serviceType, key);
}
public IEnumerable<object> GetAllInstances(Type serviceType)
{
return Container.ResolveAll(serviceType);
}
public TService GetInstance()
{
return Container.Resolve();
}
public TService GetInstance(string key)
{
return Container.Resolve(key);
}
public IEnumerable GetAllInstances()
{
return Container.ResolveAll();
}
}
Make sure you run "Install-Package Unity.Mvc3" on NuGet's PM Console. The author listed it as the first instruction in this article.
DeleteThis line of code will need it:
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
Well done - stuck to basics for those of us who are not yet Web API geniuses!
ReplyDeleteHi, Andrew! I've a problem with GlobalConfiguration which is highlighted in red.
ReplyDeleteGlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
Any idea?
Thx
using System.Web.Http;
DeleteHi Andrew, Great post!
ReplyDeleteI've got couple of questions :
1. Can i inject the API controllers instead of the services by registering them?
2. Can i register the services via the web.config in the unity configuration sections?
thx,
Gadi.
Hello Gadi,
Delete> 1. Can i inject the API controllers instead of the services by registering them?
Why do you need it?
>2. Can i register the services via the web.config in the unity configuration sections?
Yes, you can. See: http://blogs.msdn.com/b/benoitsarie/archive/2010/09/18/unity-2-configuration-file-and-unity-part-2.aspx
I'm getting an error regarding missing default constructor for the values api controller.
ReplyDeletehow does the valuesServices is resolved?
meaning how is it transfered to this controller?
I've included the sources to this topic. Enjoy.
DeleteSaved my time. Appreciate explaining the WebAPI part to us DI numbskulls. :-)
ReplyDeleteHi Andrew,
ReplyDeleteCan I use this for MVC3? Should I make any changes?
Thanks.
Yes, you can. But there is no WebAPI in MVC3.
DeleteThis comment has been removed by the author.
ReplyDeleteInstead of
ReplyDeleteprivate readonly Dictionary _values;
public ValuesService()
{
_values = new Dictionary
{
{1, "Value 1"},
{2, "Value 2"},
{3, "Value 3"},
{4, "Value 4"},
{5, "Value 5"},
};
}
I would like to do something like this (sending dbcontext as parameter to the constructor):
private readonly IEfContext _context;
public ValuesService(IEfContext _context)
{
_context = context;
}
how should i modifi the following line:
container.RegisterType();
in the Bootstrapper class?
Thanks.
Really good thanks, simplifies what some seems to make sound over complicated. Cheers
ReplyDeleteThank you Sir. That helped me out a bunch.
ReplyDeleteAny hints on how to use that with VS2013, MVC5 and WebAPI 5 ?
ReplyDeleteI am stuck there too. Any help on using Unity container on MVC5 WebAPI please? I was using NInject and it is broken when i migrate to VS 2013. Then i decided to switch to Unity thinking everything will go smooth but here i am stuck. Any help will be appreciated.
DeleteReally appreciate it. Couldn't find anything simple like this taking about DI implementation.
ReplyDeleteBe careful with UnityDependencyResolver!!!
ReplyDeleteWe have had huge issues using it with Entity Framework DbContexts. It boils down to scope and how unity wasn't creating new DbContexts for each web request. You need a new DbContext for each request else you will start getting tough Entity Framework issues due to the reuse of DbContext over mulitple requests.
If you use unity to inject DbContext to your controllers, see this post and implement UnityResolver : IDependencyResolver from the post:
http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver
Steve
Great Article..
ReplyDeleteWeb API 2.2 Training
Online Web API 2.2 Training
Online Web-API Training from India
Web-API Training in Chennai
Dot Net Training in Chennai
ASP.Net MVC Training
Online MVC Training
Online MVC Training from India
MVC Training in Chennai
Thank you for your guide to with upgrade information.
ReplyDeleteDot Net Online Training Bangalore
nice blog
ReplyDeletedata science training in bangalore
aws training in bangalore
IOT Training in Bangalore - Live Online & Classroom
ReplyDeleteIot Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Iot Training in Bangalore
Gaining Python certifications will validate your skills and advance your career.
ReplyDeletepython certification
Devops is not a Tool.Devops Is a Practice, Methodology, Culture or process used in an Organization or Company for fast collaboration, integration and communication between Development and Operational Teams. In order to increase, automate the speed of productivity and delivery with reliability.
ReplyDeletepython training in bangalore
aws training in bangalore
artificial intelligence training in bangalore
data science training in bangalore
machine learning training in bangalore
hadoop training in bangalore
devops training in bangalore
corporate training companies
ReplyDeletecorporate training companies in mumbai
corporate training companies in pune
corporate training companies in delhi
corporate training companies in chennai
corporate training companies in hyderabad
corporate training companies in bangalore