1
我已經構建了一個創建服務的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);
「創建服務A」大幹快上時,容器創建,因爲它試圖重新構圖富,我不希望它可以將上述組成的最後一行調用兩次。
有沒有人有解決這個問題?我已經明確地指定了AllowRecomposition = false,但這也不起作用。