即使将项目导入目录中,Mef 也不会导出
尝试在代码中进行导入,所以我尝试这样做:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论