I started a new MVC4 project and wanted to use StructureMap for my IOC container of choice. I followed Phil’s post about wiring up dependency resolvers in mvc 4, but I kept getting the following error: “StructureMapDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.
Parameter name: commonServiceLocator”
Took a little digging, but I found the CommonServiceLocator project from the Microsoft Patterns and Practice group. That project has a StructureMap implementation of the IServiceLocator interface for StructureMap. After I installed the CommonServiceLocator nuget, I was able to reference IServiceLocator in my code. Taking the code from the CommonServiceLocator implementation, I ended up with the following:
The following code will setup mvc to use structuremap as the dependency resolver. Add the webactivator nuget and toss the following file in your app_start folder:
Hope this helps someone looking to do the same!
5 thoughts on “Configuring MVC 4 with StructureMap”
Your .Cast() extension method isn’t included here, or its namespace isn’t if it’s a built-in method.
Is there a working MVC4RC project you can point to that shows everything wired up, perhaps on Github?
Your .Cast() extension method isn’t included here, or its namespace isn’t if it’s a built-in method.
Is there a working MVC4RC project you can point to that shows everything wired up, perhaps on Github?
Ah, using System.Linq; solves the Cast issue – http://msdn.microsoft.com/en-us/library/bb341406.aspx
And … of course you would also need the following line in global.cs : )
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(ObjectFactory.Container);
More recent code is available at: https://github.com/jgeurts/MvcKickstart/blob/master/Modules/MvcKickstart/Infrastructure/StructureMapDependencyResolver.cs
Also, the dependency resolver is set via WebActivator: https://github.com/jgeurts/MvcKickstart/blob/master/Modules/MvcKickstart/Nuget/App_Start/IocConfig.cs.pp
Installing the MVCKickstart nuget will auto wire all of that up for you.