c# - 从所有程序集中在 CaSTLe Windsor 容器中以编程方式注册所有 Controller

标签 c# asp.net-mvc castle-windsor

我用这个代码...

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

... 在容器中注册我的 Controller ,但我希望能够注册所有程序集中的所有 Controller ,以使事情更具可插拔性。我认为下面的代码应该有效,但它不起作用?

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

最佳答案

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

如果没有,做这样的事情:

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

关于c# - 从所有程序集中在 CaSTLe Windsor 容器中以编程方式注册所有 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238727/

相关文章:

c# - IEnumerable<T> 包装器的 GetEnumerator() 的替代实现

asp.net-mvc - ASP.NET MVC URL 路由没有给我漂亮的 URL

asp.net-mvc - 使用 CaSTLe Windsor 在 ASP.NET MVC 中实现 Multi-Tenancy 的最佳实践是什么?

c# - 从 Prism 开始

c# - MySQL Entity Framework 按日期分组和选择

.net - .Net 中的什么设置最类似于 Django

azure windows 应用程序服务中容器中的 asp.net mvc

asp.net-mvc - 在 MVC2 中使用 FluentValidation 与 CaSTLe Windsor 和 Entity Framework 4.0 (POCO)

c# - IoC - 单一接口(interface)的多种实现支持

c# - 更新 XML 文件 c#