MEF 子容器的重组

发布于 2024-10-19 07:43:56 字数 1070 浏览 1 评论 0原文

我构建了一个用于创建服务的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-10-26 07:43:56

您想通过父/子容器实现什么目标?在您显示的代码中,您实际上并没有创建父容器和子容器,而是创建了两个不相关的容器。要实际创建子容器,请执行以下操作:

var childContainer = new CompositionContainer(childCatalog, parentContainer);

然后父容器中的任何内容都将自动在子容器中可用(因此您不必通过批处理添加它)。您需要为子容器使用不同的目录,其中包含您想要在子容器中的部件。

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:

var childContainer = new CompositionContainer(childCatalog, parentContainer);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文