MEF / Unity依赖关系解决方案在多个线程上失败

时间:2011-09-27 00:23:49

标签: c# locking unity-container mef

我的网络应用程序使用Unity for DI。 webapp的另一部分使用MEF。代码在单线程会话中工作正常。但是,只要我从两个不同的网络会话(CHrome和FIrefox)点击相同的链接。我不确定这是循环引用循环问题还是线程问题......它在单个会话中工作正常,只有在两个不同会话中单击同一链接时才会出现此错误。

我收到以下错误:

Exception information: 
    Exception type: ResolutionFailedException 
    Exception message: Resolution of the dependency failed, type = "MefContrib.Integration.Unity.Extensions.TypeRegistrationTrackerExtension", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - Currently composing another batch in this ComposablePartExportProvider. Only one batch can be composed at a time.

有关错误的更多细节:

    Server Error in '/' Application.
    GetExportedValue cannot be called before prerequisite import 'MyApp.Core.Services.RuleService..ctor (Parameter="productService", ContractName="MyApp.Core.Services.IProductService")' has been set. 


InvalidOperationException: GetExportedValue cannot be called before prerequisite import 'MyApp.Core.Services.RuleService..ctor (Parameter="productService", ContractName="MyApp.Core.Services.IProductService")' has been set.]
   System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.EnsureGettable() +422
   System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.GetExportedValue(ExportDefinition definition) +208
   System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition) +131
   System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(ComposablePart part, ExportDefinition export, Boolean isSharedPart) +102
   System.ComponentModel.Composition.Hosting.CatalogExport.GetExportedValueCore() +148
   System.ComponentModel.Composition.Primitives.Export.get_Value() +76
   System.ComponentModel.Composition.ReflectionModel.ImportingItem.Cast(Type type, Export export) +63
   System.ComponentModel.Composition.ReflectionModel.ImportingItem.CastSingleExportToImportType(Type type, Export export) +149
   System.ComponentModel.Composition.ReflectionModel.ImportingItem.CastExportsToSingleImportType(Export[] exports) +163
   System.ComponentModel.Composition.ReflectionModel.ImportingItem.CastExportsToImportType(Export[] exports) +118
   System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.SetImport(ImportingItem item, Export[] exports) +74
   System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.SetImport(ImportDefinition definition, IEnumerable`1 exports) +230
   System.ComponentModel.Composition.Hosting.PartManager.TrySetImport(ImportDefinition import, IEnumerable`1 exports) +91
   System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportSubset(PartManager partManager, IEnumerable`1 imports, AtomicComposition atomicComposition) +449
   System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportsStateMachine(PartManager partManager, ComposablePart part) +571
   System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImports(PartManager partManager, ComposablePart part, Boolean shouldTrackImports) +277
   System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImports(ComposablePart part) +201
   System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition) +77
   System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(ComposablePart part, ExportDefinition export, Boolean isSharedPart) +102
   System.ComponentModel.Composition.Hosting.CatalogExport.GetExportedValueCore() +148
   System.ComponentModel.Composition.Primitives.Export.get_Value() +76
   System.ComponentModel.Composition.ExportServices.GetCastedExportedValue(Export export) +54
   System.ComponentModel.Composition.<>c__DisplayClass10`2.<CreateSemiStronglyTypedLazy>b__d() +107
   System.Lazy`1.get_Value() +136
   MefContrib.Integration.Unity.Strategies.CompositionStrategy.PreBuildUp(IBuilderContext context) +361
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:110
   Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\BuilderContext.cs:215
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilderCustomization\NamedTypeDependencyResolverPolicy.cs:51
   BuildUp_MyApp.Core.Services.ClientService(IBuilderContext ) +591
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs:37
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:43
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:110
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:511

[ResolutionFailedException: Resolution of the dependency failed, type = "MyApp.Core.Services.ClientService", name = "IFClientService".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - GetExportedValue cannot be called before prerequisite import 'MyApp.Core.Services.RuleService..ctor (Parameter="productService", ContractName="MyApp.Core.Services.IProductService")' has been set.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving MyApp.Core.Services.ClientService,IFClientService
  Resolving parameter "companyClientValidator" of constructor MyApp.Core.Services.ClientService(MyApp.Core.Repository.IClientRepository repository, MyApp.Core.Validators.IClientValidator validator, MyApp.Core.Validators.IChildClientValidator childClientValidator, MyApp.Core.Services.IReferenceService referenceService, MyApp.Core.Services.IProductService productService, MyApp.Core.Services.IOccupationService occupationService, MyApp.Core.Services.IAdviserService adviserService, MyApp.Core.Validators.ICompanyClientValidator companyClientValidator, MyApp.Core.Repository.IAddressRepository addressRepository)
    Resolving MyApp.Core.Validators.ICompanyClientValidator,(none)



    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: GetExportedValue cannot be called before prerequisite import 'MyApp.Core.Services.RuleService..ctor (Parameter="productService", ContractName="MyApp.Core.Services.IProductService")' has been set.

1 个答案:

答案 0 :(得分:3)

你是如何构建CompositionContainer的?默认情况下,容器不构造为线程安全。有一个重载的构造函数接受一个标志来指定它是否应该被构造为线程安全的:

CompositionContainer(ComposablePartCatalog catalog, bool threadSafe, params ExportProvider[] providers)