How to use MEF 2 Preview in .NET4 project

Aug 18, 2010 at 7:24 PM

Hi

I'm trying to use MEF2 preview in my .NET4 project. Is it possible at all? It looks like it should but I can't get it working.

I remove all references to System.ComponentModel.Composition 4.0.0.0 from .net4 and add reference to System.ComponentModel.Composition.Codeplex 4.0.0.1.

But codeplex version has the same assembly name "System.ComponentModel.Composition" (file named "System.ComponentModel.Composition.Codeplex" but assembly "System.ComponentModel.Composition"). So for referencing the difference only in revision number.

If I set "Specific verion" to "True" in Properties window then the reference is marked as unresolved (with exclamation mark). Not working.

If I set "Specific verion" to "False" (as default) and "Copy local" to "True" then in runtime I get TypeLoadException on attempting to load ExportFactory<T> from System.ComponentModel.Composition (4.0.0.0) (I'm using Prism v2 CTP with MEF integration btw). Not working.

So how to get it working?

p.s. yeah, probably the issue is not specific to mef, but anyway..

 

 

 

 

 

 

 

 

 

 

 

 

 

Aug 18, 2010 at 8:05 PM

I believe in this case you'd have to recompile everything that references Mef 4.0, to reference the codeplex version.

Aug 18, 2010 at 10:52 PM
Sure, I did this. I replaced all references to mef4 in all projects onto references to codeplex version. This doesn't help.
Aug 19, 2010 at 12:46 AM

Could you post the exception or compiler message?

Aug 19, 2010 at 1:01 AM
Edited Aug 19, 2010 at 1:24 AM

TypeLoadException: Could not load type 'System.ComponentModel.Composition.ExportFactory`1' from assembly 'System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I checked in Modules window that System.ComponentModel.Composition/4 actualy was loaded, instead of System.ComponentModel.Composition/4.0.0.1 (Codeplex).
I checked that System.ComponentModel.Composition.Codeplex is placed near the executing assembly (as I set Copy Local=True).

 

Server stack trace:

at System.Signature.GetSignature(SignatureStruct& signature, Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Signature..ctor(IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.get_ReturnType()
at System.ComponentModel.Composition.ReflectionModel.ReflectionProperty.get_ReturnType()
at System.ComponentModel.Composition.AttributedModel.AttributedModelDiscovery.GetImportDefinitionCreationInfo(ReflectionItem item, ICustomAttributeProvider attributeProvider)
at System.ComponentModel.Composition.AttributedModel.AttributedModelDiscovery.CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
at System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.GetImportDefinitions()
at System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.DiscoverExportsAndImports()
at System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.GetExports()
at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePartDefinition.get_ExportDefinitions()
at System.ComponentModel.Composition.Hosting.TypeCatalog.CreateIndex()
at System.Lazy`1.CreateValue() Exception rethrown at [0]:
at System.Signature.GetSignature(SignatureStruct& signature, Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Signature..ctor(IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.get_ReturnType()
at System.ComponentModel.Composition.ReflectionModel.ReflectionProperty.get_ReturnType()
at System.ComponentModel.Composition.AttributedModel.AttributedModelDiscovery.GetImportDefinitionCreationInfo(ReflectionItem item, ICustomAttributeProvider attributeProvider)
at System.ComponentModel.Composition.AttributedModel.AttributedModelDiscovery.CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
at System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.GetImportDefinitions()
at System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.DiscoverExportsAndImports()
at System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.GetExports()
at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePartDefinition.get_ExportDefinitions()
at System.ComponentModel.Composition.Hosting.TypeCatalog.CreateIndex()
at System.Lazy`1.CreateValue() at System.Lazy`1.LazyInitValue() at System.Lazy`1.get_Value()
at System.ComponentModel.Composition.Hosting.TypeCatalog.GetCandidateParts(ImportDefinition definition)
at System.ComponentModel.Composition.Hosting.TypeCatalog.GetExports(ImportDefinition definition)
at System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetExports(ImportDefinition definition)
at System.ComponentModel.Composition.Hosting.DirectoryCatalog.<>c__DisplayClass1.<GetExports>b__0(ComposablePartCatalog catalog)
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
at System.ComponentModel.Composition.Hosting.AggregateCatalog.GetExports(ImportDefinition definition)
at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)
at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at System.ComponentModel.Composition.Hosting.AggregateExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)
at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)
at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExports(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)
at System.ComponentModel.Composition.Hosting.CompositionContainer.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)
at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type, Type metadataViewType, String contractName, ImportCardinality cardinality)
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(Type type, Type metadataViewType, String contractName)
at Microsoft.Practices.Composite.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)

Aug 19, 2010 at 1:21 AM
Edited Aug 19, 2010 at 9:09 AM

I run fuslogvw.exe and checked what's going on.
Here's the log.
Actually my assembly asks for 4.0.0.1 but gets 4.0.0.0:

=== Pre-bind state information ===
LOG: User = SPARK\Администратор
LOG: DisplayName = System.ComponentModel.Composition, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///D:/Projects/xxx/Sources/WinClient/FrontEnd.Shell/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = FrontEnd.exe
Calling assembly : xxx.FrontEnd.Modules.AuthByFingerprint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: D:\Projects\xxx\Sources\WinClient\FrontEnd.Shell\bin\Debug\FrontEnd.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Switch from LoadFrom context to default context.
LOG: Binding succeeds. Returns assembly from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ComponentModel.Composition\v4.0_4.0.0.0__b77a5c561934e089\System.ComponentModel.Composition.dll.
LOG: Assembly is loaded in default load context.

Oct 8, 2010 at 12:24 AM

Were you ever able to sort this out? If so, how?

Oct 20, 2010 at 1:06 AM

This should've been fixed in the newest drop. Let me know if you're still having problems.