2. Why care?
Diagnosing composition failure can be tricky at first.
Root cause often misidentified by exceptions and tracing.
Better tools are available now.
Better exception and tracing are coming in MEF 2.
3. What do we already know about
MEF?
Hopefully most of you have seen Karl’s introduction to
MEF.
Key points:
MEF is used to compose parts.
A motor is part of a car.
MEF will reject a part if any required nested part is missing.
To MEF, no such thing as a “car without a motor”.
Or, if its not usable, it doesn’t exist.
Not all parts are required, you can have options
Sun roof is optional, car works without it, but you can only have
one.
Cup holders are optional, but you can have many.
4. What does MEF failure look like?
Composition exception – your module explodes and is
unusable.
You see this when you ask MEF for a part it can’t provide.
The fact that there are no wheels available for the car is
fine… until someone orders a car.
Wrong behavior – Composition succeeds, but desired
plugins/services are not loaded.
You see this when asking for a part that has optional parts
which MEF can’t provide.
Car still works without cup holders… but customer wont
accept the car without them.
6. Pizza Factory
[Export(typeof(IPizzaFactory))]
public class PizzaFactory : IPizzaFactory
Pizza Kitchen would have {
been a better name [ImportMany]
Wants to import Pizza public IEnumerable<IPizzaMaker> PizzaMakers
Makers (recipes) {
It wont ever be rejected get;
set;
But it might not know }
how to make the pizza
the customer orders. public IPizza MakePizza(string pizzaType)
{
Lets look at some // ...
diagnostics }
}
7. Debugger Inspection
Pros Cons
Easy way to diagnose Requires debugger,
simple scenarios. source code, symbols
You can see what made Hard to read.
it into the catalog.
Large catalogs harder to
If the catalog is small, navigate.
you can probably figure
Limited use when dealing
out what’s missing.
with a “collection of
Comes in the box. unknowns”.
No analysis.
8. Trace Messages
Pros Cons
Logs rejected parts. Logs the last cause of
failure, not the root
Does not require a
cause.
debugger.
Tells lies.
Comes in the box.
TMI
No analysis.
9. MEF Explorer (MEFx)
Pros Cons
Cool name Trouble with type load
exceptions.
Separate EXE provides
static analysis for Command line
(almost) any MEF parameters don’t respect
application. the working directory.
Open Source (MS-PL) Output can be hard to
read in command
window.
10. Visual MEFx
Pros Cons
Cooler name, has Beware of type load
“Visual” in it. exceptions
GUI GUI
Easier to read.
Easier to play with
“what-if” scenarios.
Open Source
(mefcontrib-tools, MS-
PL)
11. Composition Diagnostics API
Pros Cons
Same library used by Integration tests less
MEFx and VMEFx useful in “true” plug-in
Easy to integrate with scenario.
tests Test/Production catalogs
ApprovalTests can “read” must be kept in sync.
the output for you.
Works with CI when you
know what the
composition should look
like.
Open Source (MS-PL)
12. MEF 2
The part “FunnySubtitle” was rejected because the part
“FunnySpeaker” could not be satisfied.
13. Diagnostics in MEF 2
Better exceptions
Improved formatting
Root cause is shown (for real this time)
Unstable composition
Container option.
Causes exception to be thrown for any part that
would have been rejected.
Great for applications that just use MEF for
composition, and not for extensibility.
15. Satisfaction Checklist
Do your parts have the [Export] attribute?
Are your matching parts missing from the catalog?
Are there too many matching parts in the catalog?
Did you forget to export using the part’s interface name?
Do your contract names match?
Does each import meta data key exist on the export?
Does the type of the meta data on the import match the
type of the meta data on the export (1 != “1”)
Are your parts using the right creation policy?
16. Satisfaction Checklist
Can your container satisfy all nested imports?
Did the container reject your change during
recomposition?
Do your parts reference unavailable assemblies?
Are there conflicts between different versions of the
same assembly?
Do your assemblies target the expected CPU?
Are your assemblies available in your current load
context?
Do you have any other type load issues?
17. References
How to Debug and Diagnose MEF
Failures, Daniel Plaisted's Blog
http://blogs.msdn.com/b/dsplaisted/archi
ve/2010/07/13/how-to-debug-and-
diagnose-mef-failures.aspx
Diagnosing Composition Problems,
mef.codeplex.com
http://mef.codeplex.com/wikipage?title=D
ebugging%20and%20Diagnostics&referrin
gTitle=Guide
18. Outline
Why should you care about MEF
diagnostics?
What do we already know about MEF?
How might MEF fail?
How might we diagnose MEF failures?
Editor's Notes
Step0: Run default – no exception. Run MEFx from the command line.