Created
April 1, 2014 21:00
-
-
Save jbogard/9923103 to your computer and use it in GitHub Desktop.
This file contains 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 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