开发者

How to resolve the error "Currently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time."

I am getting the following error in my application that leverages MEF:

Curr开发者_如何学Cently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time.

There seems be very little information on how to fix this error, and not sure what other information I should provide to even garner some assistance.

Has anyone else received this error using MEF? If so, what were some of the ways you resolved the issue?


I've found that if you're trying to build lists of Exports to match Import types on the fly, container.GetExports<T>().Select(y => y.Value) works a lot better.

With container.ComposeParts(...) I had to have all sorts of lock(){} blocks and another thread to do the composing. GetExports<> doesn't seem to suffer from this.


While initializing you composition container, there is a flag to enable thread-safe option. As soon as you enable this one, you do not need any custom lock mechanism

var c = new AggregateCatalog();
c.Catalogs.Add(new AssemblyCatalog("MyAssemblyName"));
var container = new CompositionContainer(c, true); // true means that it is thread safe


maybe there are too many threads are using this ComposeParts function at the same time,I just locked this this function ,threads can operate this function sychonized,it goes well.

class AAA{
private  static object obj=new object();
private CompositionContainer container ;
private void MefOpt()
{
    var c = new AggregateCatalog();
    c.Catalogs.Add(new AssemblyCatalog("MyAssemblyName"));
    container = new CompositionContainer(c, true);
    }
  private void CompPrt()
{
  lock(obj)
{
    container.ComposeParts(this);
}
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜