title: ASP.NET MVC Castle Windsor and dependency injection
body: <p>I am trying to create an ASP.NET MVC project with Castle Windsor and dependency injection. Everything is working fine expect the following:</p>
  
  <p>Global.asax</p>
  
  <pre><code>Container = new WindsorContainer();
  Container.Install(new WebInstaller(), new LoggingInstaller());
  
  var customControllerFactory = new CustomControllerFactory(Container);
  ControllerBuilder.Current.SetControllerFactory(customControllerFactory);
  </code></pre>
  
  <p>Webinstaller to register the Castle classes:</p>
  
  <pre><code>string connectionStr = GetConnectionString("MainModel");
  container.Register(Component.For(typeof(MainModelContainer)).DependsOn(new { 
            connectionString = connectionStr }).LifestylePerWebRequest());
  container.Register(Classes.FromAssemblyContaining&lt;MemberQueries&gt;().InNamespace("User.EF.Queries").WithService.DefaultInterfaces().LifestylePerWebRequest());
  container.Register(Classes.FromAssemblyContaining&lt;HomeController&gt;().BasedOn&lt;IController&gt;().LifestylePerWebRequest());
  
  //PROBLEM that doesn't work
  container.Register(Classes.FromAssemblyContaining&lt;HomeController&gt;().BasedOn&lt;IAddMessage&gt;().LifestylePerWebRequest());
  </code></pre>
  
  <p>HomeController</p>
  
  <pre><code>public IAddMessage AddMessage { get; set; }
  private readonly IMemberQueries _memberContract;
  
  public HomeController(IMemberQueries memberContract)
  {
    _memberContract = memberContract;
  }
  
  public ActionResult Index()
  {
     //PROBLEM AddMessage is Null and is not resolved automaticaly
     AddMessage.Add("Test", MessageTypes.message);
     return View("Index");
  }
  </code></pre>
  
  <p>IAddMessage Implementation</p>
  
  <pre><code>public class CustomMessage : IAddMessage
  {
      private IController Controller { get; set; }
  
      public CustomMessage(IController controller)
      {
          Controller = controller;
      }
  
      public void Add(string text, MessageTypes messageTypes)
      {
          //TODO
      }
  }
  </code></pre>
  
  <p>IAddMessage Interface</p>
  
  <pre><code>public interface IAddMessage
  {
      void Add(string text, MessageTypes messageTypes);
  }
  </code></pre>
  
  <p>The problem is, that the IAddMessage property is null, because the container can't resolve the IController dependency in the CustomMessage constructor. But I don't know how to resolve this Problem.</p>
  
  <p>The internal error message in the Castle container shows this:</p>
  
  <p>Some dependencies of this component could not be statically resolved.
  'User.Web.UI.Helper.Messages.CustomMessage' is waiting for the following dependencies:
  - Service 'System.Web.Mvc.IController' which was not registered.</p>
  
  <p>I looked a lot in the web but I can't find any working hints or Solution for my problem.</p>