尝试使用 MEF 进行依赖注入并出现错误

Trying to do Dependency Injection with MEF and getting errors

设置依赖项注入后,我收到基数不匹配的 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

}

我知道您没有问过这个问题,但如果您对 MEF 没有硬性依赖,我可能建议您改用 SimpleInjector。 MEF 已经过时了,如果您只需要一个 IOC 框架,那么使用 SimpleInjector 会让您的生活变得无限美好。

容器组成

 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;     
     }

创建容器

static void Main(string[] args)
     {
         var container = CreateContainer();
         IClient contactList = container.GetExportedValue<IClient>();

         // More code below, but error is happening on the above line
     }

客户端导入

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
    }

逻辑导入

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

}