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沒有任何依賴項被導入 –
無論如何增加了數據,以防萬一有問題 –