I am in the process of integrating StructureMap into my MVC3 application. I am running into a problem where my custom ControllerFactory is throwing an exception when attempting to create a controller:
StructureMap Exception Code: 200 Could not find an Instance named "contentpage" for PluginType System.Web.Mvc.IController
Here’s what I have in the container:
Controller (System.Web.Mvc.Controller)
Scoped as: TransientAuctionCMS.Web.Controllers.HomeController, AuctionCMS.Web, Version=1.0.0.0, Culture=neutral,
AuctionCMS.Web.Controllers.ContentPageController, AuctionCMS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Configured Instance of AuctionCMS.Web.Controllers.ContentPageController, AuctionCMS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
I did notice that the controller factory is requesting “contentpage” instead of “contentpagecontroller.” Is this correct behavior? What is going wrong?
Here’s my code:
private void InitStructureMap()
{
var container = new StructureMap.Container();
DependencyResolver.SetResolver(new StructureMapContainer(container));
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));
PerformRuntimeDepdendencyConfiguration(container);
}
private void PerformRuntimeDepdendencyConfiguration(IContainer container)
{
container.Configure(x => x.Scan(y =>
{
y.TheCallingAssembly();
y.WithDefaultConventions();
y.LookForRegistries();
y.AddAllTypesOf<Controller>();
}));
}
public class StructureMapContainer : IDependencyResolver
{
static IContainer _container;
public StructureMapContainer(IContainer container)
{
_container = container;
}
public object GetService(Type serviceType)
{
if (serviceType.IsAbstract || serviceType.IsInterface)
{
return _container.TryGetInstance(serviceType);
}
else
{
return _container.GetInstance(serviceType);
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.GetAllInstances<object>().Where(s => s.GetType() == serviceType);
}
}
public class StructureMapControllerFactory : IControllerFactory
{
private readonly IContainer _container;
public StructureMapControllerFactory(IContainer container)
{
_container = container;
}
public IController CreateController(RequestContext requestContext, string controllerName)
{
System.Diagnostics.Debug.WriteLine(_container.WhatDoIHave());
return _container.GetInstance<IController>(controllerName.ToLowerInvariant());
}
public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Default;
}
public void ReleaseController(IController controller)
{
return;
}
}
public class ApplicationRegistry : Registry
{
public ApplicationRegistry()
{
... register some types ...
}
}