Skip to content

Instantly share code, notes, and snippets.

@jbogard
Created April 1, 2014 21:00
Show Gist options
  • Save jbogard/9923103 to your computer and use it in GitHub Desktop.
Save jbogard/9923103 to your computer and use it in GitHub Desktop.
public class SMDependencyResolver : SMDependencyScope, IDependencyResolver {
public SMDependencyResolver(IContainer container) : base(container) { }
public IDependencyScope BeginScope() {
var child = Container.GetNestedContainer();
return new SMDependencyScope(child);
}
}
public class SMDependencyScope : IDependencyScope {
public SMDependencyScope(IContainer container) {
Container = container;
}
public IContainer Container { get; private set; }
public object GetService(Type serviceType) {
return Container.GetInstance(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType) {
return Container.GetAllInstances(serviceType).Cast<object>();
}
public void Dispose() {
Container.Dispose();
Container = null;
}
}
// Then in your web API config global:
config.DependencyResolver = new StructureMapDependencyResolver(IoC.Container);
// IoC.Container is a Lazy<Container> for me
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment