c# - MEF错误 "Only one batch can be composed at a time"

标签 c# azure mef azure-web-roles

我有一个网络角色,可以在 Azure 中提供一个网站。此站点使用[Import] 属性导入数据存储库。这在大多数情况下工作得很好,但在中等负载下,偶尔会出现错误,提示 Currently compose another batch in this ComposablePartExportProvider。一次只能组成一批

如何修复此错误?它已经成为应用程序中一个相当有问题的瓶颈,我确信有一个(相对)快速修复...我应该注意,我正在 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设置为线程安全的,所以我不知所措。默认情况下,创建策略是共享的,因此设置 SatifsyImportsOnce 不会执行任何操作。

完全错误:

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

最佳答案

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

在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);

关于c# - MEF错误 "Only one batch can be composed at a time",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859909/

相关文章:

c# - 使用 C# 从 WMP 中检索歌曲名称

c# - 读/写压缩二进制数据

azure - 在 azure blob 存储中写入应用程序日志文件

c# - 使用MEF加载插件时如何使用Hangfire?

c# - MEF 和抽象工厂

c# - MEF 容器层次结构和 GetExports<T>

c# - 将 ASP.NET MVC 操作参数名称映射到另一个名称

c# - 处理异步异常

Azure 存储迁移

c - 如何在Raspberry Pi上运行Azure IoT C SDK的端到端测试?