MEF 子容器的重组
我构建了一个用于创建服务的 MEF 容器。我使用第一个容器中的这些服务构建另一个 MEF 容器。问题是这些服务在添加到第二个容器时会被重组。
[Export(typeof(IFoo))]
public class Foo : IFoo
{
[Import(typeof(IServiceA))]
public IServiceA A { get; set; }
}
[Export(typeof(IServiceA))]
public class ServiceAImpl : IServiceA
{
public ServiceAImpl()
{
Console.Out.WriteLine("Service A Created");
}
}
//Create the parent container
var parentContainer = new CompositionContainer(Composer.AggregateCatalog);
IFoo foo = parentContainer.GetExportedValue<IFoo>();
//..... some work
//Create a child container providing it an existing instance of IFoo
var childContainer = new CompositionContainer(Composer.AggregateCatalog);
var batch = new CompositionBatch();
batch.AddPart(foo); //Add existing IFoo
//This causes a recomposition of IFoo resulting in
//a new instance of IServiceA to be created and injected
childContainer.Compose(batch);
当容器创建组合时,“Service A Created”在上面的最后一行被调用两次,因为它试图重新组合 Foo,而我不希望它这样做。
有人有解决办法吗?我也尝试过明确指定AllowRecomposition = false,但这也不起作用。
I've built a MEF container that creates a service. I build another MEF container with these services from the first container. The problem is those services get recomposed when being added to the second container.
[Export(typeof(IFoo))]
public class Foo : IFoo
{
[Import(typeof(IServiceA))]
public IServiceA A { get; set; }
}
[Export(typeof(IServiceA))]
public class ServiceAImpl : IServiceA
{
public ServiceAImpl()
{
Console.Out.WriteLine("Service A Created");
}
}
//Create the parent container
var parentContainer = new CompositionContainer(Composer.AggregateCatalog);
IFoo foo = parentContainer.GetExportedValue<IFoo>();
//..... some work
//Create a child container providing it an existing instance of IFoo
var childContainer = new CompositionContainer(Composer.AggregateCatalog);
var batch = new CompositionBatch();
batch.AddPart(foo); //Add existing IFoo
//This causes a recomposition of IFoo resulting in
//a new instance of IServiceA to be created and injected
childContainer.Compose(batch);
"Service A Created" gets called twice on the last line above when the container creates the composition because it's trying to recompose Foo, which I don't want it to.
Does anyone have a solution to this? I've tried explicitly specifying AllowRecomposition=false too but that doesn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想通过父/子容器实现什么目标?在您显示的代码中,您实际上并没有创建父容器和子容器,而是创建了两个不相关的容器。要实际创建子容器,请执行以下操作:
然后父容器中的任何内容都将自动在子容器中可用(因此您不必通过批处理添加它)。您需要为子容器使用不同的目录,其中包含您想要在子容器中的部件。
What are you trying to achieve with parent/child containers? In the code you show, you're not actually creating a parent and a child, you're creating two unrelated containers. To actually create a child container, do the following:
Then anything in the parent will automatically be available in the child (so you won't have to add it via a batch). You'll want a different catalog for the child container with the parts you want to be in the child.