0

This is probably a stupid question! I am being forced to use Castle Windsor as my IOC and am having some issues getting things setup with MVC. Here is what I have.

GLOBAL.ASAX

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        RegisterCastle();

    }
    private void RegisterCastle()
    {
        _container = new WindsorContainer();
        _container.Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)));
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container.Kernel));
    }

The controller factory works but that is about it. I have a separate project with my installers as well I wold like it to load any installers from assemblies in the current web project(I may need something there ya know).

The classes in the DI project using IWindsorInstaller are not being loaded at all. Am I missing something?

In Ninject we could use

 kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
3
  • Are your installers public with a default, parameter-less constructor? Dec 23, 2011 at 14:10
  • Yes public class MyInstaller:IWindsorInstaller
    – Code Jammr
    Dec 24, 2011 at 15:39
  • So, to answer your questions, No -- it doesn't look like you're doing anything wrong. Could you provide more detail about what isn't working? Dec 24, 2011 at 16:08

1 Answer 1

0

I ended up using WebActivator in App_Start

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
using DFW.Domain.Interfaces;
using UI.App_Start;
using UI.Windsor;

[assembly: WebActivator.PostApplicationStartMethod(typeof(Bootstrapper), "Wire")]
[assembly: WebActivator.ApplicationShutdownMethod(typeof(Bootstrapper), "DeWire")]

namespace UI.App_Start
{
    public class Bootstrapper
    {
        private static readonly IWindsorContainer Container = new WindsorContainer();
        public static void Wire()
        {
            //To be able to inject IEnumerable<T> ICollection<T> IList<T> T[] use this:
            //container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, true));
            //Documentation http://docs.castleproject.org/Windsor.Resolvers.ashx

            //To support typed factories add this:
            Container.AddFacility<TypedFactoryFacility>();
            Container.Register(Component.For<IServiceFactory>().AsFactory().LifestyleTransient());
            //Documentation http://docs.castleproject.org/Windsor.Typed-Factory-Facility.ashx

            Container.Install(FromAssembly.This()).Install(FromAssembly.Named("APP.Infrastructure.DependencyResolution"));
            var controllerFactory = new WindsorControllerFactory(Container.Kernel);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }

        public static void DeWire()
        {
            Container.Dispose();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.