DirectoryCatalog fails strangely when trying to load my dll

Aug 9, 2009 at 10:04 PM
Edited Aug 9, 2009 at 10:11 PM

Hi there.

I started fiddeling today with this IoC and Dependency injection etc in my WPF apps using MEF. Great stuff.

I got the basic concept working nicely. Then I changed my catalog to a AggregateCatalog so that I could included the directory "Plugins". I then created a new C# class library project and implemented my little test interface there. With everything exported nicely etc. the dll is ready to be used as a "plugin" ( or extension or whatever you want to call it ). Now. I manually copied this file into a Plugins directory that I created in the Debug (or bin) folder.

Ok so we are ready to go. I launch my application from VS and bam there pops this weird Visual Studio window that says: "The following module was built either with optimizations or without debug information. C:\Users\zool\documents\...\bin\debug\plugins\yourstringfactory.dll" followed by some useless information about changing my dll's build configuration to debug (which it is) OR to disable the message with the 'Warn if no user code on launch' option. (which I cannot find)

So the DirectoryCatalog is trying to load the DLL, but it fails to load any files or parts resulting in the ImportCardinalityMismatchException later on when everything is glued together. Unlucky.

So I tried many different things, including loading the application .exe itself (which also contain the exports) and that worked. So I could get the parts from my application assembly just fine, but not some other assembly I created.

 

Any Ideas?

Aug 9, 2009 at 10:51 PM
Edited Aug 9, 2009 at 11:02 PM
pompomJuice wrote:

Hi there.

I started fiddeling today with this IoC and Dependency injection etc in my WPF apps using MEF. Great stuff.

I got the basic concept working nicely. Then I changed my catalog to a AggregateCatalog so that I could included the directory "Plugins". I then created a new C# class library project and implemented my little test interface there. With everything exported nicely etc. the dll is ready to be used as a "plugin" ( or extension or whatever you want to call it ). Now. I manually copied this file into a Plugins directory that I created in the Debug (or bin) folder.

Ok so we are ready to go. I launch my application from VS and bam there pops this weird Visual Studio window that says: "The following module was built either with optimizations or without debug information. C:\Users\zool\documents\...\bin\debug\plugins\yourstringfactory.dll" followed by some useless information about changing my dll's build configuration to debug (which it is) OR to disable the message with the 'Warn if no user code on launch' option. (which I cannot find)

So the DirectoryCatalog is trying to load the DLL, but it fails to load any files or parts resulting in the ImportCardinalityMismatchException later on when everything is glued together. Unlucky.

So I tried many different things, including loading the application .exe itself (which also contain the exports) and that worked. So I could get the parts from my application assembly just fine, but not some other assembly I created.

 

Any Ideas?

 Ok I kind of resolved that issue. The Visual Studio Window was caused because I did not copy the debug .pdb file with the dll. The second ImportCardinalityMismatchException was caused because the parts were, well, broken or something.

So that leads to my next question:

The parts that fit together. Must they be in the same assembly or map 1 to 1 or something? I ask because in my .exe I have a dependent and a property. I added the plugin folder to my catalog that results in another property to load. Therefore, my catalog has 3 parts: 1 dependent and 2 properties. Both properties satisfy the dependent.

CompesitionContainer.GetExport fails with such a catalog when trying to glue things togeher.

My question is: Is this working as intended? Can CompesitionContainer not determine which of the 2 properties it should use to complete the dependent?

 

Regards,

Coordinator
Aug 10, 2009 at 11:48 PM

It doesn't matter what assembly the parts are in, as long as they are all in the catalog.

It sounds like the problem is that you are exporting two items, but the import requests exactly one.  In this case MEF does not have any way of choosing which one should be used, so the composition fails.  What you can do is change your Import to an ImportMany and select which one to use in the importer.

See this blog post for details about how to do this, and other options: http://blogs.msdn.com/dsplaisted/archive/2009/08/10/import-cardinality-and-picking-which-export-to-use.aspx

Thanks,
Daniel 

Aug 16, 2009 at 9:24 PM
dsplaisted wrote:

It doesn't matter what assembly the parts are in, as long as they are all in the catalog.

It sounds like the problem is that you are exporting two items, but the import requests exactly one.  In this case MEF does not have any way of choosing which one should be used, so the composition fails.  What you can do is change your Import to an ImportMany and select which one to use in the importer.

See this blog post for details about how to do this, and other options: http://blogs.msdn.com/dsplaisted/archive/2009/08/10/import-cardinality-and-picking-which-export-to-use.aspx

Thanks,
Daniel 

Well done.

That was the problem, exporting more than one item. Got it fixed now ;)

Thanks.