In order to make Managed Extensibility Framework (MEF) a part of your Windows Azure project in Visual Studio 2010 while having a Test Project to test your code there is a nasty reference issue you have to resolve. I posted all the steps to resolving this issue in my recent post MEF + Azure + Visual Studio 2010 = Tricky but doable. but I forgot one step! Here is the final piece of the puzzle.
The remaining problem
Following the steps in my previous post (link above) you are good to go with your project in all respects save one. You can compile. You can run your Windows Azure application in the Development Fabric. You can run your tests in your Test Project.
The only thing you cannot do is refer from your tests to the type Tuple<T1, T2> (or Lazy<T>) in your Test Project. This will cause an “Ambiguous reference” error at compile time in your code:
Test Projects in Visual Studio 2010 have to run on .NET Framework 4.0. This new version of the framework includes the new Tuple class. However you cannot use .NET Framework 4.0 components on Windows Azure (until something like next month when Windows Azure upgrades).
Your Windows Azure project has to run on .NET Framework 3.5. Which means you have to download and run Preview 9 of MEF from the CodePlex site (link above) and use that. In my first post on this topic I showed how to get your preview (.NET 3.5) version into your (.NET 4.0) Test Project. If you follow these instructions you can compile and run your application.
However the final problem in this matter is the following: The type "’System.Tuple<T1, T2> exists in both ‘c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll’ and <your MEF assembly>.

There is a way to fix this issue so that you can refer to System.Tuple from your Test Project.
The Solution
What you need to do is eliminate one of the references. Since you can’t really, in any easy way, eliminate the .NET 4.0 version of mscorlib it’s perhaps best to try with the other one – the MEF (.NET 3.5) assembly.
Here are the steps to follow:
- Download the source code to MEF Preview 9 from the CodePlex site.
- Open up the ComponentModel Project. (Make sure you don’t get it converted to .NET Framework 4.0!)
- Open the three files Tuple, Lazy and LazyOfTTMetadata. The first two are part of mscorlib 4.0 and the last one is part of the 4.0 version of MEF (System.ComponentModel.Composition.dll).
- Rename the namespace to something you like; I chose System2 – why not?
- Make the ComponentModel Project compile again. This is done by resolving all of the references from the old System.Tuple<T1, T2>, System.Lazy<T> and System.Lazy<T, TMetadata> to the new System2.Tuple<T1, T2>, System2.Lazy<T> and System2.Lazy<T, TMetadata>.
- Once the project compiles; take this modified version of MEF (the System.ComponentModel.Composition.dll) and reference it to your solution instead of the real MEF Preview 9 version.
- Now of course you have to resolve the usages of the three types that have moved again.
That’s it! Now your Windows Azure Project will be able to run MEF Preview 9 on .NET Framework 3.5 together with your Visual Studio 2010, .NET Framework 4.0, Test Project and both will be able to reference your new version of Tuple, and the two Lazies.
Summary
Sure this is a fix but it enables you to run MEF for open ended extensibility scenarios in Windows Azure right now. When Windows Azure upgrades to .NET Framework 4.0 you can simply drop this fix and refer to the .NET Framework 4.0 version of System.ComponentModel.Composition.dll.
Cheers,
M.
posted @ Thursday, April 29, 2010 10:19 PM