views:

1668

answers:

2

I'm trying to implement Steven Sanderson's WinsorControllerFactory from his pro Asp.Net MVC Framework book (great book, btw) and I'm stumbling into an issue. I'm not sure what else you'll need to know in order to formulate a response but I greatly appreciate any help in this. Thanks!

Here's the code:

WindsorControllerFactory

public class WindsorControllerFactory : DefaultControllerFactory
{
    private WindsorContainer _container;

    public WindsorControllerFactory()
    {
        _container= new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
        var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                              where typeof (IController).IsAssignableFrom(t)
                              select t;
        foreach(Type t in controllerTypes)
        {
            _container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
        }
    }
    protected override IController GetControllerInstance(Type controllerType)
    {
        return (IController)_container.Resolve(controllerType);
    }
}

Web.Config

  <castle>
    <components>
      <component id="MenuRepository"
                 service="****.IMenuRepository, ****.Model"
                 type="****.FakeMenuRepository, ****.Model">
      </component>
      <component id="NewsRepository"
                 service="****.INewsRepository, ****.Model"
                 type="****.FakeNewsRepository, ****.Model">
      </component>
    </components>
  </castle>

NewsArticleController

public class NewsArticleController : Controller
{
    private INewsRepository _repository { get; set; }
    public NewsArticleController(INewsRepository repository)
    {
        _repository = repository;
    }

Global.asax

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.SetControllerFactory((new WindsorControllerFactory()));
    }

ERROR MESSAGE No component for supporting the service ****.NewsArticleController was found Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service ****.NewsArticleController was found

Source Error:

Line 29:         protected override IController GetControllerInstance(Type controllerType)
Line 30:         {
Line 31:             return (IController)_container.Resolve(controllerType);
Line 32:         }
Line 33:     }
+1  A: 

The S#arpArchitecture project has a decent implentation of using Windsor as a DI framework (for controllers or anything else), with little or no need to add web.config sections - http://code.google.com/p/sharp-architecture/

Code examples:

CastleWindsor/ComponentRegistrar.cs:

public class ComponentRegistrar
{
    public static void AddComponentsTo(IWindsorContainer container) {
        AddGenericRepositoriesTo(container);
        AddCustomRepositoriesTo(container);

        container.AddComponent("validator",
            typeof(IValidator), typeof(Validator));
    }

    private static void AddCustomRepositoriesTo(IWindsorContainer container) {
        container.Register(
            AllTypes.Pick()
            .FromAssemblyNamed("Northwind.Data")
            .WithService.FirstNonGenericCoreInterface("Northwind.Core"));
    }

    private static void AddGenericRepositoriesTo(IWindsorContainer container) {
        container.AddComponent("entityDuplicateChecker",
            typeof(IEntityDuplicateChecker), typeof(EntityDuplicateChecker));
        container.AddComponent("repositoryType",
            typeof(IRepository<>), typeof(Repository<>));
        container.AddComponent("nhibernateRepositoryType",
            typeof(INHibernateRepository<>), typeof(NHibernateRepository<>));
        container.AddComponent("repositoryWithTypedId",
            typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
        container.AddComponent("nhibernateRepositoryWithTypedId",
            typeof(INHibernateRepositoryWithTypedId<,>), typeof(NHibernateRepositoryWithTypedId<,>));
    }
}

Global.asax (primary initialization method for di):

protected virtual void InitializeServiceLocator() {
        IWindsorContainer container = new WindsorContainer();
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

        container.RegisterControllers(typeof(HomeController).Assembly);
        ComponentRegistrar.AddComponentsTo(container);

        ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
    }
mannish
I need to clarify that the S#arpArchitecture implentation has some other framework bits that are part of the SharpArch assemblies. The code snippets above are from their Northwind example MVC app.
mannish
Thanks man, I figured out what the problem was but I'll check this out as well. I had my WindsorController factory in a Util project just assumed that the references to the assembly would be from the point of initialization and not within the class itself. So it was basically searching the util directory for controllers (and found none).Goffy mistake on my part
Chance
+1  A: 

MvcContrib offers some extension methods for registering controllers in Windsor, example:

windsorContainer.RegisterControllers(Assembly.GetExecutingAssembly());

Mannish's code uses the same extension method.

Igor Brejc