This project has moved and is read-only. For the latest updates, please go here.

Debugging Incorrect Import

Mar 10, 2009 at 4:38 AM
I'm still struggling with debugging MEF. Simple problems taking hours and trying to refine my strategy. In this case...

I have a field filled by an Import


private IT4PropertyDirective _propertyDirective;

When I atttempt to use the value of this field,the value is null. As it does allow default,this implies to me that the Import was not evaluated, rather than evaluated and no match found as that should be error.

Another class in this assembly is successfully found so I know the catalog is working correctly. Doesn't that cause all the classes in the assembly to be evalauted for Import resoution?

For grins I added an Export to the class that contains the field and that didn't work.

So what to look for when the value of an Import is null? The Import appears to be set up correctly to create an instance of the correct class.

Mar 10, 2009 at 4:42 AM
I'm tired...

As it does NOT allow default (as shown)
Mar 10, 2009 at 4:47 AM
Sorry, one more thing...

Is it relevant how the class is created? This code is in a directive processor in a T4 engine. The T4 library calls a host to resolve a directive processor which returns a Type. The T4 library code then instaniates the directive processor (where the Import is not being evaluated). It's a boatload of ugly code to make T4 work, and I'd like to take the meat of it and MEF-ify.
Mar 10, 2009 at 5:40 AM
Hi Kathleen

You mentioned that it allows default, do you mean it should allow default? In order to specify an optional import, you need to set the AllowDefault parameter of the Import attribute. The fact that you have not done that and it is not null, leads me to believe that that part itself which has the private import is not being composed. Does your part have a non-default constructor? If so did you put the ImportingConstructorAttribute on it?

Here's a few tips for debugging.

1 One thing you can do in order to test whether composition is happening at all, is to implement the INotifyImportSatisfaction interface on your part. Everyime we compose a part, we look to see if it implements that interface. If it does, then we will invoke the ImportCompleted() method on the part after we have satisfied any imports.
2. You can also view the Parts property on the catalog in the debugger, in order to see which Parts were actually discovered. The Parts collection contains Part Defintiions, and each PD contains Import and Export definitions. Once you've found your part then you can drill into the imports to see which imports we have discovered.

In terms of needing the Export attribute on the part, you are correct in this. We use the Export to indicate a part's "partness". If you do not have any exports, we won't discover.

HTH Glenn