Share via


Castle Windsor IOC : No component for supporting the service Web.Controllers.HomeController was found

Question

Thursday, October 6, 2011 4:45 AM

Hi folks,

Has any got any idea of what I'm doing wrong here? I'm trying to inject a class called ProgrammeService (which itself takes an instance of IPlatrformRespository in its construtor) into my HomeController using asp.net MVC 3 but I get a 'No component for supporting the service Web.Controllers.HomeController was found. First I overwrote the defaultcontrollerfactory in MVC wiith the below code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Windsor;
using MvcContrib.Castle;
using System.Web.Routing;

namespace Web.Controllers
{
    /// <summary>
    /// see http://docs.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx
    /// </summary>
    public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IWindsorContainer _container;
 
        public WindsorControllerFactory(IWindsorContainer container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null) return base.GetControllerInstance(requestContext, controllerType);
            var controller = (Controller)_container.Resolve(controllerType);
            return controller;
        }

        public override void ReleaseController(IController controller)
        {
            _container.Release(controller);
        }
    }
}

and then I create a windsorcontainer 

 public class BootStrapper
    {
        public static IWindsorContainer Container;
        
        public static void ConfigureDependencies()
        {
            Container = new WindsorContainer();
  
            Container.Register(Component.For<IWindsorContainer>().Instance(Container));
            Container.AddComponent("HomeController", typeof(IPlatformRepository), typeof(PlatformRespository));
            
            ControllerBuilder.Current.SetControllerFactory(new Web.Controllers.WindsorControllerFactory(Container));
           
        }
    }

and call configureDependencies from my global.asax file. What am I missing. I know with structuremap I don't have to explicity mention ProgrammeService itself as it is dependent on IPlatformRespositroy and that is there, but if you've any ideas please advise. I'm also wondering about the whereabouts of the 'FromAssembly' helper class. Some tutorials mention it but I can never get it to resolve, no matter what namespaces I include.

Thanks in advance.

All replies (2)

Thursday, October 13, 2011 2:13 AM

I think it's worth to check the call stack of the exception and find out which function is the excption from. it doesn't look like a MVC error message.


Friday, July 26, 2013 1:18 AM

You're registering the controller as "HomeController". This is not the name. Try this registration instead:

container.Register(Castle.MicroKernel.Registration.Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestylePerWebRequest()
.Configure(x => x.Named(x.Implementation.FullName)));