I’m trying to follow this tutorial using StructureMap : http://iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Action-Filters.aspx
What I’m trying to figure out is the StructureMap equivalent of this line:
var container = (ICanResolveDependencies) HttpContext.Current.ApplicationInstance;
I want to get the container back, so I can resolve dependencies there manually.
This is how I am setting the dependencyresolver in global.asax
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
new StructureMapDependencyResolver(container));
You could just require
IContainerdependency in your action filter’s constructor. If it’s not registered automatically byStructureMap, you could register it with:Edit
Option 1: Couldn’t you just use something like:
There got to be some way of retrieving the current service resolver once you set it.
Option 2: With regular MVC you can get the current resolver like this:
And use it like this:
Looks like WebAPI doesn’t use DependencyResolver, but according to this blog post, you could set it like this:
Now try to use
DependencyResolver.Currentfrom your action filter.Option 3: Use
ObjectFactory.GetInstancedirectly – Probably not the best idea in MVC project, since it should already be encapsulated in IDependencyResolver instance.