MEF错误“一次只能组合一个批次”

3
我有一个 Web 角色,它在 Azure 中提供网站。该站点使用 [Import] 属性导入数据存储库。这在大多数情况下都可以正常工作,但在中等负载下,偶尔会出现错误,提示 Currently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time
如何解决这个错误?这已成为应用程序中相当棘手的瓶颈问题,我相信有一个(相对)快速的解决方法... 我应该注意到我在 Nuget 中使用了 MEF.MVC4 组件。
我的 Global.asax.cs 调用 RegisterMefApplication_Start() 上设置 MEF 方面的事情。
public static void RegisterMef()
{
    var container = ConfigureContainer();
    ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
    GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver(container);
}

private static CompositionContainer ConfigureContainer()
{
    var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
    var container = new CompositionContainer(assemblyCatalog, true);
    return container;
}

我的CompositionContainer已设置为线程安全,所以我不知所措。默认情况下创建策略是共享的,因此设置SatisfyImportsOnce不会有任何作用。

完整错误信息:

Currently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: Currently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 


    [InvalidOperationException: Currently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time.]
       System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch batch) +390
       MEF.MVC4.MefControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +126
       System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +81
       System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +270
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +86
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12550511
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

你能解决这个问题吗? - overslacked
1
@overslacked - 我们不得不放弃MEF... - Faraday
1个回答

0

我通过不使用MEF.MVC4来解决这个问题,因为它的实现在中等负载(例如:100个用户同时请求)时可能存在错误。我使用MEF2 这里

在Application_Start()中

 var catalog = new AggregateCatalog(new DirectoryCatalog("bin"));
 var container = new CompositionContainer(catalog, true);
 ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
 GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver(container);
 CurrentContainer.SetCurrentContainer(container);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接