서비스를 만드는 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를 지정하려고 시도했지만 역시 작동하지 않습니다.