MEF“出口是不能分配给键入”错误 [英] MEF 'The export is not assignable to type' error

查看:254
本文介绍了MEF“出口是不能分配给键入”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MEF我刚刚起步,并没有击中一个早期的问题。

I have just started using MEF and have hit on an early problem.

我有一个名为DataService的接口:

I have an interface called DataService:

namespace DataAccess
{
  interface IDataService
  {
    string Name { get; }
    string Description { get;}

    List<String> GetPeople();
  }
}

有2实现此界面,一种用于SQL服务器和一个用于Oracle。
下面是Oracle实施,SQL Server实现是完全一样的。

There are 2 implementations of this interface, one for SQL Server and one for Oracle. Below is the Oracle implementation, SQL Server implementation 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
}
}

名称和描述特性现已解散的,因为我曾与元数据取代这些。正如你所看到的,他们是非常简单的对象,我想确保我能得到这个之前,我开始做的辛苦工作。

The name and description properties are now defunct as I have replaced these with metadata. As you can see, they are very simple objects, I wanted to make sure I could get this to work before I started doing the hard work.

这是我的代码现在用发现的组件:

This is the code I am using to discover the 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());
    }

这是产生错误:

的组合物生产的单一组合物的错误。根本原因如下。查看更详细的信息,CompositionException.Errors属性。

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1)出口DataAccess.Oracle(ContractName =DataAccess.IDataService)'是不能分配给键入' DataAccess.IDataService。

1) The export 'DataAccess.Oracle (ContractName="DataAccess.IDataService")' is not assignable to type 'DataAccess.IDataService'.

在所得:无法设置进口'的一部分MEFTest.Form1MEFTest.Form1.services(ContractName =DataAccess.IDataService)。
元素:MEFTest.Form1.services(ContractName =DataAccess.IDataService) - > MEFTest.Form1

Resulting in: Cannot set import 'MEFTest.Form1.services (ContractName="DataAccess.IDataService")' on part 'MEFTest.Form1'. Element: MEFTest.Form1.services (ContractName="DataAccess.IDataService") --> MEFTest.Form1

这似乎没有任何意义它不能分配给它设计的界面!

It doesn't seem to make any sense that it can't assign to the interface that it was designed for!

一旦这个问题解决了,我的下一个问题是如何选择一个,并得到它的一个实例...

Once this problem is solved, my next issue is how to pick one and get an instance of it...

推荐答案

看起来越来越加载两个不同版本的合同组件(一个与DataAccess.IDataService)的。其中一个可能是从你的可执行文件路径,并从你的插件路径等。我在这个问题上摸了一下上的如何调试和诊断故障MEF 以及在最佳的MSDN页面装配装载实践进入更多的细节。

It looks like two different versions of your contract assembly (the one with DataAccess.IDataService) are getting loaded. One is probably from your executable path and the other from your plugin path. I touch on this issue a bit in my blog post on How to Debug and Diagnose MEF Failures, and the MSDN page on Best Practices for Assembly Loading goes into more detail.

这篇关于MEF“出口是不能分配给键入”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆