So I fired up MVC4 today excited to get started with APIController.
As usual the first two nuget packages I loaded were RavenDB and StructureMap. I noticed that there was a nuget package for structuremap called structuremap.mvc4 which said it worked out of the box with MVC4 Api Controllers.
I kept getting Activation Exceptions like:
Activation error occured while trying to get instance of type IHttpControllerFactory, key “”
After a little research here is how I solved it and what I learned.
In MVC3 we were given an new interface IDependencyResolver which lives in System.Web.Mvc. By implementing this interface , our IOC container could plug directly into the framework as follows:
DependencyResolver.SetResolver(new Your IOC’s Implementation);
With MVC4 there is a second resolver which needs to be configured using:
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new Your IOC’s Implementation);
Where this gets a little confusing is that this also takes an IDependencyResolver but NOT the one which lives in System.Web.Mvc. It needs to IDependencyResolver which lives in System.Web.Http.Services.
The interfaces are identical, they are just in different namespaces.
StructureMap has a built in ServiceLocator called StructureMapServiceLocator which is the default when using the nuget package. I could not get this to work.
Instead I used a little adapter class from Phil Haack’s Blog
which looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Now you can modify the second service resolver to use this adapter class
With the nuget package, this all lives in the App_StartStructureMapMvc.cs Here is the finished version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18