1
设置我的依赖注入后,我得到与基数不匹配的MEF错误。我相信我正在从接口正确导出,当我检查程序时,实际目录是空的。不知道我做错了什么!试图做与MEF的依赖注入和获取错误
集装箱组成
private static CompositionContainer CreateContainer()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IClient).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
return 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
}
客户端导入
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
}
逻辑进口
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
}
是否'ImportingConstructor'需要有一个构造函数的参数'Import'属性? – Fabio
是的,已更新以包括逻辑以及.. dataAccess没有任何依赖项被导入 –
无论如何增加了数据,以防万一有问题 –