title: Injecting dependency into MVC controller with Spring .NET
body: <p>The object in the controller is not getting injected at run time.</p>
  
  <p>Web.config:</p>
  
  <pre><code>    &lt;sectionGroup name="spring"&gt;
          &lt;section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /&gt;
          &lt;section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /&gt;
      &lt;/sectionGroup&gt;
  </code></pre>
  
  <p>.
  .
  .</p>
  
  <pre><code>&lt;!-- Spring Context Configuration --&gt;
  &lt;spring&gt;
      &lt;context&gt;
          &lt;resource uri="config://spring/objects"/&gt;
      &lt;/context&gt;
      &lt;objects configSource="App_Config\Spring.config" /&gt;
  &lt;/spring&gt;
  &lt;!-- End Spring Context Configuration --&gt;
  </code></pre>
  
  <p>Spring.config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;objects xmlns="http://www.springframework.net"&gt;
  
      &lt;!-- Crest is the WCF service to be exposed to the client, could not use a singleton --&gt;
  
      &lt;object id="TestController" type="Project.Controllers.TestController, Project" singleton="false"&gt;
          &lt;property name="ObjectA" ref="ObjectImpl"/&gt;
      &lt;/object&gt;
  
      &lt;object id="ObjectImpl" type="Project.Code.Implementations.ClassA, Project" singleton="false" /&gt;
  
  &lt;/objects&gt;
  </code></pre>
  
  <p>TestController:</p>
  
  <pre><code>public class TestController: Controller
      {
          // this object will be injected by Spring.net at run time
          private ClassA ObjectA { get; set; }
  </code></pre>
  
  <p>Problem:</p>
  
  <blockquote>
    <p>At runtime the ObjectA does not get injected and stays null, which
    cause null exception throughout the code.</p>
  </blockquote>
  
  <p>Alternative:
  I can manually initialize the Spring object and get it's object using the following code.</p>
  
  <pre><code>        var ctx = ContextRegistry.GetContext();
          var objectA = ((IObjectFactory)ctx).GetObject("ObjectImpl") as ClassA;
  </code></pre>