0
votes

I'm having a lot of trouble getting structuremap to work properly with asp.net mvc 4. Now, I know it DOES work (I had it working before, but I don't have access to that particular codebase any more). I have a library for this solution that has a bunch of repository classes in it and currently also has a few Structuremap Registry classes for setting up the repositories and a few other things. Now, on the web side, I can initialize that part just fine and I've proven that it works by calling ObjectFactory.GetInstance in one of my controller classes. I'm able to get the list of users.

However, I need constructor injection to work for the controllers. I installed the structuremap mvc 4 package, and I noticed that it put some stuff in App_Start and a few other places. Nothing was placed in Global.asax. Should something be there to set the container up for MVC? Or am I missing some other subtle step here?

1

1 Answers

0
votes

After some troubles, I resolved it. I had an object previously, called Bootstrapper, that did all the IOC setup. So I had to modify StructureMapMvc.cs to the following (basically, telling it to use the existing container):

public static class StructuremapMvc {
    public static void Start()
    {
        Bootstrapper.Bootstrap();
        var container = ObjectFactory.Container;
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(ObjectFactory.Container));
        GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
    }