StructureMapMvcApplication

Apr 13, 2011 at 7:41 PM

My question is about how to use StructureMap inside of a StructureMapMvcApplication.  I understand how to wire everything up using "RegisterServices.cs" and have no trouble getting the my projects to work this way.  But usually you can make a call to ObjectFactory.GetInstance<>(); for any object you have registered with StructureMap and get that object back.  Whenever I try this I get back NULL or a StructureMap error.  BUT, if I add IContainer as a parameter to the constructor of whatever object I am working in like so:

private readonly IContainer _container;

public MyClass(IContainer container) {

    _container = container;

}

public void MyMethod() {

    var service = _container.GetInstance<IService>();

}

Everything works as expected.  I realize I could put IService as a constructor parameter and have access to it but sometimes you need to get access to an object as needed with ObjectFactory.GetInstance<>();  Thanks for the help!