| Author |
Message |
JohnRedGater
Joined: 27 Jun 2011 Posts: 10
|
Posted: Thu Jul 28, 2011 3:41 am Post subject: Strange MEF SmartAssembly behavior |
|
|
I'm using MEF to find and load a plugin from the main directory. In the debug mode, which isn't obfuscated, the composition fails because it can't find any plugins in the directory and throws an error. This is expected behavior. In the release mode, which IS obfuscated, however, the composition doesn't throw any errors and proceeds leaving a null reference for the Import property. Subsequent calls get a null exception when they try to access the property. This is odd behavior; MEF should at least fail in somewhere in the composition process. Here's my code:
[Import(typeof(IPlugin))]
public IPlugin Plugin
{
get;
set;
}
public FindPlugins(string directory)
{
// should throw a composition exception
if (String.IsNullOrEmpty(directory))
directory = DirectoryOfExecutingAssembly;
var catalog = new DirectoryCatalog(directory);
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
} |
|
| Back to top |
|
 |
Brian Donahue
Joined: 23 Aug 2004 Posts: 6344 Location: Red Gate Software
|
Posted: Fri Jul 29, 2011 1:07 pm Post subject: |
|
|
Hello,
If IPlugin is defined in another assembly, and you are obfuscating or pruning that assembly separately or merging it into the main assembly, you may be making the metadata for IPlugin inaccessable. I'd suggest excluding that type from obfuscation and pruning.
I don't know a lot about how MEF works internally, but it is probably designed to catch an exception when the requested type is unknown. _________________ Brian Donahue
Technical Support
Red Gate Software Ltd.
44 (0)870 160 0037 ext 8521
US and CAN 1-866-RED GATE ext 8521 |
|
| Back to top |
|
 |
JohnRedGater
Joined: 27 Jun 2011 Posts: 10
|
Posted: Sat Jul 30, 2011 3:34 am Post subject: |
|
|
Thanks. I discovered that indeed pruning was the problem, but it didn't completely solve my MEF woes. I still couldn't get my plugin to load.
Here's the process according to my poor recall. Originally, I MERGED the interface assembly into the main assembly. This made the interface assembly invisible to the plugin. I then tried to EMBED the interface assembly into the main assembly. Although it found the plugin, it couldn't find the plugin's referenced assemblies which were merged into the main assembly. I tried several combinations and I couldn't get them to work.
After several different setup arrangements, I found that I had to embed the plugins references into to the main assembly but leave the interface outside of both assemblies. Magically, this works and the plugin is able to reference the assemblies in the main assembly. The resulting plugin is very small and references everything it needs from the main assembly. Ideally, I wish the assemblies were merged instead of embedded for better protection. Also, it would be nice to deploy two assemblies, main and plugin, instead of three: main, plugin, and interface.
Given the popularity of MEF, I would strongly advise that it is directly addressed in either the software or technical documentation. I more than happy to provide assistance in helping reproduce these issues. |
|
| Back to top |
|
 |
Brian Donahue
Joined: 23 Aug 2004 Posts: 6344 Location: Red Gate Software
|
Posted: Mon Aug 01, 2011 9:35 am Post subject: |
|
|
In a broader sense what we would like to do is incorporate some sort of static analysis of the assembly to try to ensure certain classes shouldn't be renamed because they have external dependencies or are needed for reflection. These sorts of problems affect all application types, not just MEF, and take a lot of effort to sort out. _________________ Brian Donahue
Technical Support
Red Gate Software Ltd.
44 (0)870 160 0037 ext 8521
US and CAN 1-866-RED GATE ext 8521 |
|
| Back to top |
|
 |
|
|
All times are GMT + 1 Hour
|
| Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group