开发者

MEF/Unity Dependency Resolution failing on multiple threads

开发者 https://www.devze.com 2023-04-08 02:02 出处:网络
My web application uses Unity for DI. Another section of the webapp uses MEF. The code works fine in a single threaded session. But as soon as I click on the same link from two different web sessions

My web application uses Unity for DI. Another section of the webapp uses MEF. The code works fine in a single threaded session. But as soon as I click on the same link from two different web sessions (CHrome and FIrefox). I'm not sure if this is a circular reference loop issue or a threading issue... It works just fine on a single session, this error only occurs when clicking on the same link in two different sessions.

I get the following error:

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.

More detail regarding the error:

    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\开发者_JS百科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.


How have you constructed your CompositionContainer? By default, containers are not constructed as thread safe. There is an overloaded constructor that accepts a flag to specify whether it should be constructed as thread-safe:

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

精彩评论

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

关注公众号