我正在为我的软件建立一个插件框架,我一直在使用AutoFac来简化所有依赖项解析。
问题是,当调用子容器Resolve方法时,我不知道如何解决依赖项
https://github.com/ChickenAPI/ChickenAPI.Core/blob/master/test/ChickenAPI.Core.Tests/Ioc_Test.cs
我已经尝试过模块,但是它没有按预期运行,仍然错过了childContainer.Resolve();。这不起作用
var pluginBuilder = new ContainerBuilder();
pluginBuilder.RegisterType<TestCorePlugin>().AsSelf().AsImplementedInterfaces();
IContainer container = pluginBuilder.Build();
var coreBuilder = new ContainerBuilder();
foreach (ICorePlugin plugin in container.Resolve<IEnumerable<ICorePlugin>>())
{
plugin.OnLoad()
}
coreBuilder.RegisterModule(new NLogModule());
using (IContainer coreContainer = coreBuilder.Build())
{
var gameBuilder = new ContainerBuilder();
gameBuilder.RegisterInstance(coreContainer).As<IContainer>();
gameBuilder.RegisterTypes(typeof(Ioc_Test).Assembly.GetTypesImplementingInterface<IGamePlugin>()).AsSelf().AsImplementedInterfaces();
IContainer gameContainer = gameBuilder.Build();
IEnumerable<IGamePlugin> plugins = gameContainer.Resolve<IEnumerable<IGamePlugin>>();
foreach (IGamePlugin gamePlugin in plugins)
{
gamePlugin.OnEnable();
gamePlugin.OnDisable();
}
gameContainer.Resolve<DependencyOnlyInCoreContainer>();
}
但是它仅在对象在子容器中注册并且需要一些核心容器注册的依赖项时才有效