I just started using MEF and got into an early problem.
I have an interface called DataService:
namespace DataAccess
{
interface IDataService
{
string Name { get; }
string Description { get;}
List<String> GetPeople();
}
}
There are 2 implementations of this interface: one for SQL Server and one for Oracle. The following is the implementation of Oracle, the implementation of SQL Server is exactly the same.
namespace DataAccess
{
[Export(typeof(IDataService))]
[ExportMetadata("Name","Oracle")]
[ExportMetadata("Description","Oracle Data Service")]
public class Oracle : IDataService
{
#region IDataService Members
public string Name
{
get { return "Oracle"; }
}
public string Description
{
get { return "Provides data access to Oracle database"; }
}
public List<string> GetPeople()
{
return new List<String>() { "Oracle boo", "Oracle boo1" };
}
#endregion
}
}
Name and description properties no longer function as I replaced them with metadata. As you can see, these are very simple objects, I wanted to make sure that I could get this to work before I started doing the hard work.
This is the code I use to detect assemblies:
private static CompositionContainer _container;
private const string ASSEMBLY_PATTERN = "*.dll";
private AggregateCatalog _catalog;
[ImportMany]
IEnumerable<DataAccess.IDataService> services { get; set; }
private void button3_Click(object sender, EventArgs e)
{
_catalog = new AggregateCatalog(
new DirectoryCatalog(txtLibPath.Text, ASSEMBLY_PATTERN),
new AssemblyCatalog(Assembly.GetExecutingAssembly()));
_container = new CompositionContainer(_catalog);
_container.ComposeParts(this);
MessageBox.Show(services.Count().ToString());
}
This is mistake:
The composition produced a single compositional error. The main reason is given below. See the CompositionException.Errors property for more information.
1) 'DataAccess.Oracle(ContractName = "DataAccess.IDataService" )' 'DataAccess.IDataService'.
: "MEFTest.Form1.services(ContractName =" DataAccess.IDataService ")" "MEFTest.Form1".
: MEFTest.Form1.services(ContractName = "DataAccess.IDataService" ) → MEFTest.Form1
, , , !
, , ...