"缺少依赖性和QUOT;异常MEF 2 [英] "Missing dependency" exception with MEF 2

查看:272
本文介绍了"缺少依赖性和QUOT;异常MEF 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习MEF 2.下面code抛出以下异常:

I am studying MEF 2. Below code throws below exception:

类型的未处理的异常   System.Composition.Hosting.CompositionFailedException发生在   System.Composition.TypedParts.dll

An unhandled exception of type 'System.Composition.Hosting.CompositionFailedException' occurred in System.Composition.TypedParts.dll

更多信息:缺少依赖MessageSenders   MEFStudy.Program。

Additional information: Missing dependency 'MessageSenders' on 'MEFStudy.Program'.

调用SatisfyImports()方法时。为什么呢?

when calling the SatisfyImports() method. Why?

using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Hosting;


using System.Reflection;

namespace MEFStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
        }

        [ImportMany]
        private List<IMessageSender> MessageSenders { get; set; }

        public void Run()
        {
            Compose();
            foreach (IMessageSender sender in MessageSenders)
            {
                sender.Send();
            }

        }

        private void Compose()
        {
            CompositionHost host = new ContainerConfiguration().WithAssembly(Assembly.GetExecutingAssembly()).CreateContainer();
            host.SatisfyImports(this);   // <=========== HERE
            host.Dispose();

        }
    }

    public interface IMessageSender
    {
        void Send();
    }

    [Export(typeof(IMessageSender))]
    public class EmailSender1 : IMessageSender
    {
        public void Send()
        {
            Console.WriteLine("EmailSender1");
        }
    }

    [Export(typeof(IMessageSender))]
    public class EmailSender2 : IMessageSender
    {
        public void Send()
        {
            Console.WriteLine("EmailSender2");
        }
    }

}

更新1

据这里 ,有2个版本,MEF的。

Update 1

According to here, there are 2 versions of MEF.

  • 非便携式1 shiped与.NET框架
  • 在便携式一个可用上的NuGet

名单,其中,IMessageSender&GT; 办法适用于非便携的。但不与便携式之一。这是一个错误?

The List<IMessageSender> approach works with non-portable one. But not with the portable one. Is this a bug?

推荐答案

我不小心改变后,code:

I accidentally changed the following code:

[ImportMany]
private List<IMessageSender> MessageSenders { get; set; }

[ImportMany]
private IEnumerable<IMessageSender> MessageSenders { get; set; }

和它解决了这个问题。

但尽管如此,为什么呢?是不是名单,其中,T&GT; 的IEnumerable&LT; T&GT;

But still, why? Isn't List<T> an IEnumerable<T> ?

和更奇怪,我改变了IEnumerable到IList的,它的工作原理。为什么呢?

And even stranger, I changed the IEnumerable to IList, it works. Why?

(我想与大家分享我的解释了这一点。)

(I'd like to share my explanations to this.)

下面的接口可以准确地再现了同样的错误。

The following interface can reproduce exactly the same error.

interface IMyList<T> : IList<T>
{

}

[System.Composition.ImportMany] // MEF 2
private IMyList<IMessageSender> MessageSenders { get; set; }

下面的MEF 2源显示原因。

The following MEF 2 source shows the reason.

了equals()方法IMyList&LT返回false。因此,在MEF2将不会返回IMyList&LT有效的出口;>。 和MEF 2允许饰[ImportMany]属性的属性没有默认值。因此,在下面的逻辑,依赖缺失将引发异常。

The Equals() method of 3 SupportedContactTypes returns false with IMyList<>. So in MEF2 will not return valid export for IMyList<>. And MEF 2 allows no default value for property decorated with [ImportMany] attribute. So in the following logic, the dependency missing exception will be thrown.

所以我们可以说,ImportMany属性只支持阵列和3支持泛型类型。

So we can say, ImportMany attribute only support array and the 3 supported generic types.

这篇关于&QUOT;缺少依赖性和QUOT;异常MEF 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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