There is a nuget package for it Castle.Windsor.Web.Mvc
On Monday, August 26, 2013 4:37:08 PM UTC+2, haithem khalifa wrote:
>
> 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 ?
>
>
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/groups/opt_out.