-
-
Save weslley39/7531557 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebApiApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
// *** Other Configurations *** | |
UnityConfiguration(GlobalConfiguration.Configuration); | |
} | |
public void UnityConfiguration(HttpConfiguration httpConfiguration) | |
{ | |
IUnityContainer container = new UnityContainer(); | |
container.RegisterType(typeof(IExample), typeof(Example)); | |
container.RegisterType(typeof (IOtherExample), typeof (OtherExample)); | |
httpConfiguration.DependencyResolver = new IoCContainer(container); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IoCContainer : ScopeContainer, IDependencyResolver | |
{ | |
public IoCContainer(IUnityContainer container) | |
: base(container) | |
{ | |
} | |
public IDependencyScope BeginScope() | |
{ | |
var child = container.CreateChildContainer(); | |
return new ScopeContainer(child); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScopeContainer : IDependencyScope | |
{ | |
protected IUnityContainer container; | |
public ScopeContainer(IUnityContainer container) | |
{ | |
if (container == null) | |
{ | |
throw new ArgumentNullException("container"); | |
} | |
this.container = container; | |
} | |
public object GetService(Type serviceType) | |
{ | |
if (container.IsRegistered(serviceType)) | |
{ | |
return container.Resolve(serviceType); | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
if (container.IsRegistered(serviceType)) | |
{ | |
return container.ResolveAll(serviceType); | |
} | |
else | |
{ | |
return new List<object>(); | |
} | |
} | |
public void Dispose() | |
{ | |
container.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment