I realized this thanks to the @Michael Carman link posted in a comment on his answer. I am not sure about this etiquette as to whether he justifies his actual answer, since it was not entirely correct (I gave him a +1 vote), but I thought I would send my own answer to explain the release was.
The problem was the combination of my implementation of IDependencyResolver and my container configuration. I initially had:
public class StructureMapDependencyResolver : IDependencyResolver { public object GetService(Type serviceType) { return ObjectFactory.GetInstance(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { foreach (object obj in ObjectFactory.GetAllInstances(serviceType)) { yield return obj; } } }
but now I changed it based on Steve Smith's blog post related to Jeremy Miller's Post :
public class StructureMapDependencyResolver : IDependencyResolver { public object GetService(Type serviceType) { if (serviceType.IsAbstract || serviceType.IsInterface) { return ObjectFactory.TryGetInstance(serviceType); } else { return ObjectFactory.GetInstance(serviceType); } } public IEnumerable<object> GetServices(Type serviceType) { foreach (object obj in ObjectFactory.GetAllInstances(serviceType)) { yield return obj; } } }
This alone does not solve the problem until I remove this configuration expression:
x.For<IControllerFactory>().Use<DefaultControllerFactory>();
According to the documentation, TryGetInstance returns the types registered in the container and returns null if none exist. I assume MVC 3 code relies on this behavior to indicate that it should use its default values, so in my original case I had to register these default values ββwith my container. Difficult!
source share