【问题标题】:Autofac Modules Imported with MEF not being Loaded使用 MEF 导入的 Autofac 模块未加载
【发布时间】:2015-11-15 04:53:40
【问题描述】:

我有许多程序集,每个程序集都通过使用扩展 Autofac.Module 的类向 Autofac 提供它们的依赖项。我将其中的每一个都装饰为 MEF 导出,例如:

[Export(typeof(IModule))]
public class ContainerModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);
    }
}

作为客户端应用程序启动的一部分,我收集了所有这些模块,例如:

var builder = new ContainerBuilder();
var path = "Path to application Bin directory";
var catalog = new DirectoryCatalog(path, "MyApp*.dll");

builder.RegisterComposablePartCatalog(catalog);

container = builder.Build();

查看目录时,我可以看到所有组件中的所有模块都存在。

我的问题是如何指示 Autofac 在每个加载的模块上调用 Load 方法?

我正在考虑 builder.RegisterAssemblyModules 的一些用法,但还没有(还)知道如何将其与目录联系起来。

谢谢!

r.

【问题讨论】:

    标签: mef autofac autofac-module


    【解决方案1】:

    你可能无法以这种方式做你想做的事。

    通过将部件目录与其中的模块注册,您将它们注册为组件,类似于任何其他依赖项。让它们退出的唯一方法是构建容器并解析IEnumerable<IModule>>,但是调用 Load 不会在模块中注册这些东西,因为容器已经构建了。

    如果您希望模块将其注册贡献给容器,您必须直接向 ContainerBuilder 注册它们,而不是通过 MEF 集成。

    【讨论】:

    • 真的吗?臭虫。似乎是一个如此简单的想法。谢谢。
    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2011-06-08
    • 2017-11-27
    相关资源
    最近更新 更多