Question

Here's my problem. Assembly A contains one exported interface that is imported by many class's constructors (through injection) throughout my application.

Application 1 uses Assembly A by adding it to the AggregateCatalog. Everything works as it should for Application 1. However, in Application 2, I want the value of the single part of Assembly A to be null, essentially to never be loaded. I still want any class's constructor that uses this part to still be called through mef.

So I tried excluding Assembly A from the AggregateCatalog, and consequently, many class's constructors (with ImportingConstructor attribute) never got hit because one of their parameters was the interface from Assembly A.

How can I make everything work as usual, but just have the value of the interface in Assembly A be null when used by mef.

Was it helpful?

Solution

Found the answer here: http://dailydotnettips.com/2011/09/06/importconstructor-to-inject-constructor-mef/

The AllowDefault = true for an import will set the object to its default value (null for objects) when it is unavailable in the Container thus relaxing the runtime exception to occur.

[ImportingConstructor]
public ExportContainer([Import(AllowDefault=true)]IService service)
{
     this.service= service;
}

So I was able to ignore Assembly A when I added all the assemblies to the AggregateCatalog. And this made Application 2 work as wanted!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top