I have an asp.net application using a model assembly (with a Model class) for business logic. This model assembly has a dependency on a MailService through an IMailService interface and I am trying to use MEF to fill the Models need for a MailService implementation. I am doing the MEF composition in the contructor of the Model class.
The idea behind this is to create a MailService assembly I can reuse between my websites without the sites themselves need to know how the mail is sent. Maybe an IoC container would be a better choise but I just think the MEF approach is simpler to understand and I like the idea of composing my apps by combining parts. Also does the mef approach have any negative sides if you compare with a IoC container?
[Import]
private IMailService _mailService;
public Model()
{
Compose();
}
private void Compose()
{
DirectoryCatalog cat = new DirectoryCatalog(Settings.Default.PluginsFolder);
var container = new CompositionContainer(cat);
container.ComposeParts(this);
}
The code below is in another assembly and the interface in yet another assembly
[Export(typeof(IMailService))]
public class MailService : IMailService
{
}
this works fine in my unit tests for the model assembly but when I am using the model assembly through my asp.net site it fails with the exception below. I also tried to set the trust to full in web.config but still no luck
The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) No exports were found that match the constraint '((exportDefinition.ContractName = "ExtensionInterfaces.IMailService") && (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && "ExtensionInterfaces.IMailService".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'.
Resulting in: Cannot set import Model.Model._mailService (ContractName="ExtensionInterfaces.IMailService")' on part Model.Model'. Element: Model.Model._mailService (ContractName="ExtensionInterfaces.IMailService") --> Model.Model