GetExportedValue cannot be called before prerequisite import has been set

旧时模样 提交于 2019-12-07 02:55:04

问题


We are using MEF in a WPF application.

And we are getting this error:

GetExportedValue cannot be called before prerequisite import 'MyNamespace.MyMainClass..ctor (Parameter="myParameter", ContractName="IContractInterface")' has been set.

I am not using threads, I read sometimes this error happens with multiple threads, however just in case I created the composition container passing the "thread safe" parameter in true

var container = new CompositionContainer(catalog, true);

The Code I have is:

My simplified Main Class code is: (The idea is that if there are not import for IMySubClass, I will use a default Implementation, that is why I create it on the "OmImportsSatisfied" method and satisfy its imports.)

MyMainClass C#

[Export(typeof(IMyInterface)]
public class MyMainClass: IPartImportsSatisfiedNotification, IMyInterface
{
    [Import]
    public IContainer Container { get; private set; }

    [Import(AllowDefault = true)]
    public IMySubClass MySubClass { get; set; }

    [ImportingConstructor]
    public MyMainClass([Import(AllowDefault = true)] IContractInterface myParameter= null)
        : this()
    {
        if (myParameter!= null)
        {
            //Do Something with myParameter
        }            
    }

    public void OnImportsSatisfied()
    {
        if (MySubClass == null)
        {
            MySubClass = new MySubClass();
            Container.SatisfyImportsOnce(MySubClass);
        }

        //Some other stuff
     }

}

MySubClass C#

public class MySubClass : IPartImportsSatisfiedNotification, IMySubClass
{
    [ImportMany]
    public Lazy<IMyAttributeInterface, IMyAttributeInterfaceMetadata>[] Exports { get; set; }

    public void OnImportsSatisfied()
    {
        foreach (var export in Exports)
        {
            var value = export.Value; //Here it throws the Exception
            //Do something.
        }
    }

The error is thrown in MySubClass, inside the OnImportSatisfied method on the line:

var value = export.Value;

However when I debug the ImportConstructor of MyMainClass is called successfully, and myParameter is injected and used. Afterwards the OnImportsSatisfied method is called and I get the error.


The Exports list from my Subclass comes from properties in other classes that have the Attribute "MyExportAttribute". I don't have much experience creating Export Attributes, so here is the code in case the problem is coming from it.

Export Attribute

public interface IMyAttributeInterfaceMetadata
{
    string Property1 { get; }
    string Property2 { get; }
}

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public class MyExportAttribute : ExportAttribute, IMyAttributeInterfaceMetadata
{
    string Property1 { get; }
    string Property2 { get; }

    public MyExportAttribute(string property1, string property2)
        : base(typeof(IMyAttributeInterface))
    {
        Property1 = property1;
        Property2 = property2;
    }
}

来源:https://stackoverflow.com/questions/21309467/getexportedvalue-cannot-be-called-before-prerequisite-import-has-been-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!