After setting up my dependency injection I get MEF errors with a cardinality mismatch. I believe I am correctly exporting from the interfaces and when I inspect the program the actual catalog is empty. Not sure what I am doing wrong!
Container Composition
private static CompositionContainer CreateContainer()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IClient).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
return container;
}
Create Container
static void Main(string[] args)
{
CompositionContainer container = CreateContainer();
IClient contactList = container.GetExportedValue<IClient>();
// More code below, but error is happening on the above line
}
Client Import
namespace MyWcfProgram.WcfProgram
{
[Export(typeof(IClient))]
public class Client : IClient
{
private readonly ILogic contactLogic;
[ImportingConstructor]
public Client([Import] ILogic contactLogic)
{
if(contactLogic == null)
{
throw new Exception();
} else
{
this.contactLogic = contactLogic;
}
}
// MORE BELOW LEFT OUT
}
Logic Import
namespace MyWcfProgram.Logic
{
[Export(typeof(ILogic))]
public class Logic : ILogic
{
private readonly IData dataAccess;
[ImportingConstructor]
public Logic([Import] IData dataAccess)
{
if (dataAccess == null)
{
throw new Exception();
}
this.dataAccess = dataAccess;
}
Data.cs
namespace MyWcfProgram.Data
{
[Export(typeof(IData))]
public class Data : IData
{
private string connectionString = "CONNECTION STRING IS HERE";
private System.Data.SqlClient.SqlConnection myconnection = new System.Data.SqlClient.SqlConnection();
// More code below, but no constructor since there are no dependencies
}
I know you didn't ask this but might I suggest using SimpleInjector instead if you dont have a hard dependency on MEF. MEF is antiquated and if all you need is an IOC framework you're life will be infinitely better using SimpleInjector instead.
Container Composition
private static Container CreateContainer()
{
var container = new Container();
container.Register<IClient, MyWcfProgram.WcfProgram.Client>();
container.Register<ILogic, MyWcfProgram.Logic.Logic>();
container.Register<IData, MyWcfProgram.Data.Data>();
return container;
}
Create Container
static void Main(string[] args)
{
var container = CreateContainer();
IClient contactList = container.GetExportedValue<IClient>();
// More code below, but error is happening on the above line
}
Client Import
namespace MyWcfProgram.WcfProgram
{
public class Client : IClient
{
private readonly ILogic contactLogic;
public Client(ILogic contactLogic)
{
if(contactLogic == null)
{
throw new Exception();
} else
{
this.contactLogic = contactLogic;
}
}
// MORE BELOW LEFT OUT
}
Logic Import
namespace MyWcfProgram.Logic
{
public class Logic : ILogic
{
private readonly IData dataAccess;
public Logic(IData dataAccess)
{
if (dataAccess == null)
{
throw new Exception();
}
this.dataAccess = dataAccess;
}
Data.cs
namespace MyWcfProgram.Data
{
public class Data : IData
{
private string connectionString = "CONNECTION STRING IS HERE";
private System.Data.SqlClient.SqlConnection myconnection = new System.Data.SqlClient.SqlConnection();
// More code below, but no constructor since there are no dependencies
}