Dependency injection Mef即使项目导入到目录中也不导出

Dependency injection Mef即使项目导入到目录中也不导出,dependency-injection,mef,containers,composition,Dependency Injection,Mef,Containers,Composition,尝试在代码中进行导入,因此我尝试以下操作: IServiceBehavior behavior = container.GetExport<IServiceBehavior>().Value; 但是,当我检查容器MefServiceBehavior(IServiceBehavior的一个实现)的局部变量时,目录中有: container {System.ComponentModel.Composition.Hosting.CompositionContainer} Sys

尝试在代码中进行导入,因此我尝试以下操作:

IServiceBehavior behavior = container.GetExport<IServiceBehavior>().Value;
但是,当我检查容器MefServiceBehavior(IServiceBehavior的一个实现)的局部变量时,目录中有:

container   {System.ComponentModel.Composition.Hosting.CompositionContainer}    System.ComponentModel.Composition.Hosting.CompositionContainer
    -       Catalog {MefContrib.Hosting.Generics.GenericCatalog}    System.ComponentModel.Composition.Primitives.ComposablePartCatalog  {MefContrib.Hosting.Generics.GenericCatalog}
        -       Parts   Count = 6   System.Collections.ObjectModel.ReadOnlyCollection<System.ComponentModel.Composition.Primitives.ComposablePartDefinition>
            -       [4] {MyProjectServiceLibrary.Hosting.MefServiceBehavior}    System.ComponentModel.Composition.Primitives.ComposablePartDefinition {System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePartDefinition}
                -       ExportDefinitions   {System.ComponentModel.Composition.Primitives.ExportDefinition[1]}  System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.ExportDefinition> {System.ComponentModel.Composition.Primitives.ExportDefinition[]}
                    -       [System.ComponentModel.Composition.Primitives.ExportDefinition[]]   {System.ComponentModel.Composition.Primitives.ExportDefinition[1]}  System.ComponentModel.Composition.Primitives.ExportDefinition[]
                        -       [0] {MyProjectServiceLibrary.Hosting.MefServiceBehavior (ContractName="System.ServiceModel.Description.IServiceBehavior")}  System.ComponentModel.Composition.Primitives.ExportDefinition {System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition}
                            -       [System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition]    {MyProjectServiceLibrary.Hosting.MefServiceBehavior (ContractName="System.ServiceModel.Description.IServiceBehavior")}  System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition
                                -       Metadata    Count = 1   System.Collections.Generic.IDictionary<string,object> {Microsoft.Internal.Collections.ReadOnlyDictionary<string,object>}
                                    -       [0] {[ExportTypeIdentity, System.ServiceModel.Description.IServiceBehavior]}    System.Collections.Generic.KeyValuePair<string,object>
                                                Key "ExportTypeIdentity"    string
                                                Value   "System.ServiceModel.Description.IServiceBehavior"  object {string}
container{System.ComponentModel.Composition.Hosting.CompositionContainer}System.ComponentModel.Composition.Hosting.CompositionContainer
-目录{MefContrib.Hosting.Generics.GenericCatalog}System.ComponentModel.Composition.Primitives.ComponentBaltCatalog{MefContrib.Hosting.Generics.GenericCatalog}
-零件计数=6 System.Collections.ObjectModel.ReadOnlyCollection
-[4]{MyProjectServiceLibrary.Hosting.MefServiceBehavior}System.ComponentModel.Composition.Primitives.ComponentPartDefinition{System.ComponentModel.Composition.ReflectionModel.ReflectionComponentPartDefinition}
-ExportDefinitions{System.ComponentModel.Composition.Primitives.ExportDefinition[1]}System.Collections.Generic.IEnumerable{System.ComponentModel.Composition.Primitives.ExportDefinition[]}
-[System.ComponentModel.Composition.Primitives.ExportDefinition[]{System.ComponentModel.Composition.Primitives.ExportDefinition[1]}System.ComponentModel.Composition.Primitives.ExportDefinition[]
-[0]{MyProjectServiceLibrary.Hosting.MefServiceBehavior(ContractName=“System.ServiceModel.Description.IServiceBehavior”)}System.ComponentModel.Composition.Primitives.ExportDefinition{System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition}
-[System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition]{MyProjectServiceLibrary.Hosting.MefServiceBehavior(ContractName=“System.ServiceModel.Description.IServiceBehavior”)}System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition
-元数据计数=1 System.Collections.Generic.IDictionary{Microsoft.Internal.Collections.ReadOnlyDictionary}
-[0]{[ExportTypeIdentity,System.ServiceModel.Description.IServiceBehavior]}System.Collections.Generic.KeyValuePair
键“ExportTypeIdentity”字符串
值“System.ServiceModel.Description.IServiceBehavior”对象{string}

为什么容器找不到导出。ExportTypeIdentity与异常消息中提到的相同。这是怎么回事?

我发现了一个非常有用的工具,用于调试MEF组合,名为mefx.exe,它帮助我找到了问题所在。看起来问题不在于IServiceBehavior,而在于IServiceBehavior中的一个导入,我没有将导出类型放入其中。我很高兴你解决了这个问题。我建议您通过mefx工具的链接来回答自己的问题,以帮助其他人通过搜索找到您的问题。
container   {System.ComponentModel.Composition.Hosting.CompositionContainer}    System.ComponentModel.Composition.Hosting.CompositionContainer
    -       Catalog {MefContrib.Hosting.Generics.GenericCatalog}    System.ComponentModel.Composition.Primitives.ComposablePartCatalog  {MefContrib.Hosting.Generics.GenericCatalog}
        -       Parts   Count = 6   System.Collections.ObjectModel.ReadOnlyCollection<System.ComponentModel.Composition.Primitives.ComposablePartDefinition>
            -       [4] {MyProjectServiceLibrary.Hosting.MefServiceBehavior}    System.ComponentModel.Composition.Primitives.ComposablePartDefinition {System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePartDefinition}
                -       ExportDefinitions   {System.ComponentModel.Composition.Primitives.ExportDefinition[1]}  System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.ExportDefinition> {System.ComponentModel.Composition.Primitives.ExportDefinition[]}
                    -       [System.ComponentModel.Composition.Primitives.ExportDefinition[]]   {System.ComponentModel.Composition.Primitives.ExportDefinition[1]}  System.ComponentModel.Composition.Primitives.ExportDefinition[]
                        -       [0] {MyProjectServiceLibrary.Hosting.MefServiceBehavior (ContractName="System.ServiceModel.Description.IServiceBehavior")}  System.ComponentModel.Composition.Primitives.ExportDefinition {System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition}
                            -       [System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition]    {MyProjectServiceLibrary.Hosting.MefServiceBehavior (ContractName="System.ServiceModel.Description.IServiceBehavior")}  System.ComponentModel.Composition.ReflectionModel.ReflectionMemberExportDefinition
                                -       Metadata    Count = 1   System.Collections.Generic.IDictionary<string,object> {Microsoft.Internal.Collections.ReadOnlyDictionary<string,object>}
                                    -       [0] {[ExportTypeIdentity, System.ServiceModel.Description.IServiceBehavior]}    System.Collections.Generic.KeyValuePair<string,object>
                                                Key "ExportTypeIdentity"    string
                                                Value   "System.ServiceModel.Description.IServiceBehavior"  object {string}