title: StructureMap IoC in Application_Start
body: <p>I cannot get StructureMap to inject a value into the constructor of the MvcApplication of the Global.asax.cs file. I have created a brand new and clean project and used StructureMap.MVC5 package to generate the necessary structures inside the DependencyResolution subfolder.</p>
  
  <p>My class to be injected is simple:</p>
  
  <pre><code>namespace SMTest.Models {
      public interface ITestSM
      {
          int OnePlusTwo();
      }
      public class TestSM : ITestSM
      {
          public int OnePlusTwo()
          {
              return 1 + 2;
          }
      } }
  </code></pre>
  
  <p>IoC.cs is:</p>
  
  <pre><code>namespace SMTest.DependencyResolution {
      using StructureMap;
  
      public static class IoC {
          public static IContainer Initialize() {
              return new Container(c =&gt; c.AddRegistry&lt;DefaultRegistry&gt;());
          }
      }
  }
  </code></pre>
  
  <p>StructuremapMvc.cs contains:</p>
  
  <pre><code>    public static void Start() {
          IContainer container = IoC.Initialize();
          StructureMapDependencyScope = new StructureMapDependencyScope(container);
          DependencyResolver.SetResolver(StructureMapDependencyScope);
          DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));
      }
  </code></pre>
  
  <p>And DefaultRegistry.cs contains:</p>
  
  <pre><code>    public DefaultRegistry() {
          Scan(
              scan =&gt; {
                  scan.TheCallingAssembly();
                  scan.WithDefaultConventions();
                  scan.With(new ControllerConvention());
              });
  
          For&lt;ITestSM&gt;().Singleton().Use&lt;TestSM&gt;();
      }
  </code></pre>
  
  <p>I can easily inject it into the HomeController:</p>
  
  <pre><code>public class HomeController : Controller
  {
      private ITestSM _testsm;
      public HomeController(ITestSM testsm):base()
      {
          _testsm = testsm; 
      }
  
      public ActionResult Index()
      {
          return View();
      }
  </code></pre>
  
  <p>But when I try to do that in Global.asax.cs:</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      private ITestSM _testsm;
      public MvcApplication(ITestSM testsm)
      {
          _testsm = testsm;
      }
  
      protected void Application_Start()
      {...
  </code></pre>
  
  <p>I get the error:
  <strong>Compiler Error Message: CS7036: There is no argument given that corresponds to the required formal parameter 'testsm' of 'MvcApplication.MvcApplication(ITestSM)'</strong></p>
  
  <p>Despite the fact that IoC init triggers before the Application_Start, StructuremapMvc.cs:</p>
  
  <pre><code>[assembly: PreApplicationStartMethod(typeof(StructuremapMvc), "Start")]
  [assembly: ApplicationShutdownMethod(typeof(StructuremapMvc), "End")]
  </code></pre>
  
  <p>Any idea?</p>
  
  <p><strong>Edit:</strong>
  I was able to inject it into Global.asax.cs explicitly:</p>
  
  <pre><code>    protected void Application_EndRequest(object sender, System.EventArgs e)
      {
          if (!this.Request.Path.Contains("Content") &amp;&amp; !this.Request.Path.Contains("Scripts") &amp;&amp; !this.Request.Path.Contains("less"))
          {
              ISession currentSession = SMTest.App_StartStructuremapMvc.StructureMapDependencyScope.Container.GetInstance&lt;ISession&gt;();
              currentSession.Flush();
              currentSession.Close();
          }
      }
  </code></pre>
  
  <p>However, the session I get here is not the one injected into the controller so I am flushing and closing the wrong one, leaving data not commited to the DB.</p>