Skip to content

Instantly share code, notes, and snippets.

@andrebaltieri
Created September 23, 2014 16:38
Show Gist options
  • Save andrebaltieri/6de87baaad45e8c5121c to your computer and use it in GitHub Desktop.
Save andrebaltieri/6de87baaad45e8c5121c to your computer and use it in GitHub Desktop.
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;
namespace CustomerService.Utils.Helpers
{
public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container;
public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}
public void Dispose()
{
container.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment