0

我将 DI 引入到我的 MS MVC 应用程序中,并且无法从我的自定义控制器工厂中实例化控制器。似乎没有调用覆盖的“GetControllerInstance”。

有人能告诉我我错过了什么吗?

我的控制器工厂:

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);
    }
}

引导程序:

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

国际奥委会:

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);
        }
    }

温莎注册商:

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());
    }
}

请帮忙 ?

4

2 回答 2

1

有一个 Nuget 包可以让 Windsor 无缝集成到 MVC 应用程序中。

PM> Install-Package Castle.Windsor.Web.Mvc
于 2013-08-29T08:31:36.570 回答
0

乍一看,我看不出任何错误...但是可以肯定的是,您的管道是多余的...按照温莎教程保持简单

于 2013-08-25T23:39:48.433 回答