即使将项目导入目录中,Mef 也不会导出

发布于 2024-12-10 23:17:16 字数 3184 浏览 0 评论 0原文

尝试在代码中进行导入,所以我尝试这样做:

IServiceBehavior behavior = container.GetExport<IServiceBehavior>().Value;

但是得到这个异常:

{"No valid exports were found that match the constraint '((exportDefinition.ContractName == \"System.ServiceModel.Description.IServiceBehavior\") AndAlso (exportDefinition.Metadata.ContainsKey(\"ExportTypeIdentity\") AndAlso \"System.ServiceModel.Description.IServiceBehavior\".Equals(exportDefinition.Metadata.get_Item(\"ExportTypeIdentity\"))))', invalid exports may have been rejected."}

但是当我检查容器 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}

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

Trying to do a import in code so I try this:

IServiceBehavior behavior = container.GetExport<IServiceBehavior>().Value;

but get this eexception:

{"No valid exports were found that match the constraint '((exportDefinition.ContractName == \"System.ServiceModel.Description.IServiceBehavior\") AndAlso (exportDefinition.Metadata.ContainsKey(\"ExportTypeIdentity\") AndAlso \"System.ServiceModel.Description.IServiceBehavior\".Equals(exportDefinition.Metadata.get_Item(\"ExportTypeIdentity\"))))', invalid exports may have been rejected."}

But when I check the locals for the container MefServiceBehavior, a implementation of IServiceBehavior, is there in the catalog:

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}

Why doesn't the container find the export. The ExportTypeIdentity is the same as it is mentioned in the exception message. What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文