从所有程序集中以编程方式在Castle Windsor容器中注册所有控制器

4

我使用这段代码...

        container.Register(
            AllTypes
            .FromAssembly(Assembly.Load("MyNamespace.Dashboard"))
            .BasedOn<IController>()
            .Configure(component => component.LifeStyle.Transient
            .Named(ControllerNameFromType(component.Implementation)))
            );

我想在容器中注册我的控制器,但我希望能够注册来自所有程序集的所有控制器,以使事情更具可插入性。我认为以下代码应该可以工作,但它没有?
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

        foreach (var assembly in assemblies) {
            container.Register(
                AllTypes
                .FromAssembly(assembly)
                .BasedOn<IController>()
                .Configure(component => component.LifeStyle.Transient
                .Named(ControllerNameFromType(component.Implementation)))
                );
        }

你是否遇到了异常?如果是,请发布完整的堆栈跟踪。 - Mauricio Scheffer
您的WindsorControllerFactory是标准的还是有修改过的?静态的是可以的。 - Mauricio Scheffer
我对ControllerFactory做了一些修改,它看起来像这样http://pastebin.com/m8f17255 - marcus
在 GetControllerType(string controllerName) 函数中, engine.Repository.Load() 找不到类型映射“Stormbreaker.Dashboard.Models.SysRoot”的运行时类型。这就是异常所说的。 - Mauricio Scheffer
其实我也不太清楚,我尝试继承WindsorControllerFactory,然后只覆盖GetControllerType方法,现在看起来似乎可以了。 - marcus
显示剩余4条评论
1个回答

0

检查程序集以查看是否已加载所有程序集。

如果没有,请执行以下操作:

        foreach (string dllPath in Directory.GetFiles(directoryPath, "*.dll"))
        {
            try
            {
                Assembly a = Assembly.ReflectionOnlyLoadFrom(dllPath);
                if (Matches(a.FullName) && !loadedAssemblyNames.Contains(a.FullName))
                {
                    App.Load(a.FullName);
                }
            }
            catch (BadImageFormatException ex)
            {
                Trace.TraceError(ex.ToString());
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接