How to resolve a type in global.asax with Unity.AspNet.WebApi

1.7k Views Asked by At

I've got Unity.AspNet.WebApi through NuGet into my Asp.Net WebApi application and all my controllers are able to resolve classes through constructor injection. Easy and simple.

However I am not sure on how to resolve classes in global.asax as I don't have direct access to UnityContainer here. I've tried below and it seems to be working. Is there a better way?

Also, as this is start up code not sure if this clashes with the WebActivator which registers UnityContainer. I am concerned that whether there woould be always a Dependencyresolver ready in Application_Start() method.

var o = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IService));
var service = (IService)o;
serviceInitializePermissionsCache();
1

There are 1 best solutions below

0
On

Answering my own question:

var service = UnityConfig.GetConfiguredContainer().Resolve<IService>();