MVC - Creating a Castle Windsor Controller Factory and GetControllerInstance is never called

Aug 26, 2013 at 1:12 PM
I am introducing DI into my MS MVC application and I am having trouble getting the controllers instantiated from within my custom Controller Factory. It seems that the overridden "GetControllerInstance" is not being called.

Can someone tell me what I am missing?

My Controller Factory:
public class WindsorControllerFactory : DefaultControllerFactory
    {
        readonly IWindsorContainer container;
    
        public WindsorControllerFactory(IWindsorContainer container)
        {
            this.container = container;
            var controllerTypes =
                from t in Assembly.GetExecutingAssembly().GetTypes()
                where typeof(IController).IsAssignableFrom(t)
                select t;
            foreach (var t in controllerTypes)
                container.Register(Component.For(t).LifeStyle.Transient);
        }
    
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null) return null;
            return (IController)container.Resolve(controllerType);
        }
    }



 
Bootstrap:
    public static void Bootstrap()
            {
                RouteConfigurator.RegisterRoutes(RouteTable.Routes);
                ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(IoC.Container));
                WindsorConfigurator.Configure();
            .
            .
             }




IoC:
    public static class IoC
        {
            private static readonly object LockObj = new object();
    
            private static IWindsorContainer container = new WindsorContainer();
    
            public static IWindsorContainer Container
            {
                get { return container; }
    
                set
                {
                    lock (LockObj)
                    {
                        container = value;
                    }
                }
            }
    
            public static T Resolve<T>()
            {
                return container.Resolve<T>();
            }
    
            public static object Resolve(Type type)
            {
                return container.Resolve(type);
            }
        }




WindsorRegistrar:

public class WindsorRegistrar
{
    public static void RegisterSingleton(Type interfaceType, Type implementationType)
    {
        IoC.Container.Register(Component.For(interfaceType).ImplementedBy(implementationType).LifeStyle.Singleton);
    }

    public static void Register(Type interfaceType, Type implementationType)
    {
        IoC.Container.Register(Component.For(interfaceType).ImplementedBy(implementationType).LifeStyle.PerWebRequest);
    }

    public static void RegisterAllFromAssemblies(string a)
    {
        IoC.Container.Register(AllTypes.FromAssemblyNamed(a).Pick().WithService.FirstInterface().LifestylePerWebRequest());
    }
}
HELP PLEASE ?
Coordinator
Aug 28, 2013 at 10:48 AM
the code provided looks fine, the same code that is in prodinner, there could be many reasons why it wont work, for example is the Bootstrap method being called in Global App_Start
Aug 31, 2013 at 4:45 PM
Hi
the resolve the problem by using the sample project of windsor castle but now i have a problem .an exeption is hired :
No component for supporting the service Core.Repository.IDelRepo`1[[Core.Model.User, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] was found
i test this :

    public class ControllersInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Classes.FromThisAssembly()
                                .BasedOn<IController>()
                                .LifestyleTransient());

            container.Register(Component.For(typeof(IFormsAuthentication)).ImplementedBy(typeof(FormAuthService)).LifeStyle.PerWebRequest);
            container.Register(Component.For(typeof(IHasher)).ImplementedBy(typeof(Hasher)).LifeStyle.PerWebRequest);
            container.Register(Component.For(typeof(IUserService)).ImplementedBy(typeof(Service.UserService)).LifeStyle.PerWebRequest);
            container.Register(Component.For(typeof(Core.Repository.IDelRepo<>)).ImplementedBy(typeof(Data.DelRepo<>)).LifeStyle.PerWebRequest);

            container.Register(Classes.FromAssembly(Assembly.GetAssembly(typeof(Data.Db))).Pick()
                .WithService.FirstInterface().Configure(component => component.LifestylePerWebRequest()));

            container.Register(Classes.FromAssembly(Assembly.GetAssembly(typeof(Service.FileManagerService))).Pick()
                .WithService.FirstInterface().Configure(component => component.LifestylePerWebRequest()));

            container.Register(Classes.FromAssembly(Assembly.GetExecutingAssembly()).Pick()
                .WithService.FirstInterface().Configure(component => component.LifestylePerWebRequest()));
        }
    }
normally the Core.Repository.IDelRepo shold be registred ?? i dont understand the problem ? HELP PLEASE
Coordinator
Aug 31, 2013 at 7:41 PM
try and start from scratch or just use the prodinner model and use this:
WindsorRegistrar.RegisterAllFromAssemblies("MyNamespace.Data") which will scan all the asembly register all the types with first intefrace