Reputation: 9565
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
Upvotes: 1
Views: 1304
Reputation: 9565
I found the error, I had a assembly in the bin folder with the same name as the MailService assembly with my export. For some reason the catalog picked up the assembly in the bin folder instead of the assembly in the catalog I specified. Don't know why but it worked when I removed the old assembly from the bin folder at least.
Upvotes: 1
Reputation: 4364
I've not used MEF in a web application yet, but if I were to guess I would assume there is some issue with reading the assemblies in the plugins directory. Maybe an permissions issue or something. At any rate I would start by examining the DirectoryCatalog to see if it contains what you would expect it to.
Upvotes: 1