c# – 使用特定接口时的MEF组合导入问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用特定接口时的MEF组合导入问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3777字,纯文字阅读大概需要6分钟。
内容图文
![c# – 使用特定接口时的MEF组合导入问题](/upload/InfoBanner/zyjiaocheng/786/6d26da35a5d448b4b99156a3eb7ea5b4.jpg)
我和MEF工作了很长时间,偶尔它会让我发疯.不知道它需要什么.
我有两个我们感兴趣的文件:
>我的EXE有2个班级:
JobFactory:IJobFactory和SaferWatchProcessor:IJob
>具有这些接口定义的Quartz.net DLL
创建容器:
var aggregateCatalog = new AggregateCatalog(
new DirectoryCatalog(".", "*.dll"),
new DirectoryCatalog(".", "*.exe"));
Bootstrapper.CompositionContainer = new CompositionContainer(aggregateCatalog, true);
目录现在有JobFactory但没有SaferWatchProcessor.为什么?
这是课程:
[Export(typeof(IJob))]
public class SaferWatchProcessor : IJob
{
public void Execute(IJobExecutionContext context)
{
Debug.WriteLine("SaferWatchProcessor.Execute");
}
}
SaferWatchProcessor没有任何东西,只有一种方法.有导出属性.
[Export(typeof(IJobFactory))]
public class JobFactory : IJobFactory
{
[ImportMany(typeof(IJob))]
public List<IJob> Jobs { get; private set; }
public virtual IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
IJobDetail jobDetail = bundle.JobDetail;
Type jobType = jobDetail.JobType;
try
{
Debug.WriteLine("IDATT.WindowsService.JobFactory - creating job instance");
return this.Jobs.First();
}
catch (Exception e)
{
var se = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Problem instantiating class '{0}'", jobDetail.JobType.FullName), e);
throw se;
}
}
public virtual void ReturnJob(IJob job)
{
}
}
JobFactory有ImportMany(不会失败,但有0项)
我试图将其设置为单个导入并且出现以下错误:
System.ComponentModel.Composition Warning: 1 : The ComposablePartDefinition 'IDATT.WindowsService.JobFactory' has been rejected. The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) No exports were found that match the constraint:
ContractName Quartz.IJob
RequiredTypeIdentity Quartz.IJob
Resulting in: Cannot set import 'IDATT.WindowsService.JobFactory.Jobs (ContractName="Quartz.IJob")' on part 'IDATT.WindowsService.JobFactory'.
Element: IDATT.WindowsService.JobFactory.Jobs (ContractName="Quartz.IJob") --> IDATT.WindowsService.JobFactory --> DirectoryCatalog (Path=".")
A first chance exception of type 'System.InvalidOperationException' occurred in IDATT.WindowsService.exe
似乎没有什么错,但为什么它不想导入IJob?
编辑:
我删除了所有IJob定义并使用普通的导出/导入并为MEF添加了调试.仍有问题,这里是错误:
[Part] IDATT.WindowsService.JobFactory from: DirectoryCatalog (Path="C:\CodeWorkspace\IdattLC\ClientServerCode\IDATT.WindowsService\bin\Debug\")
[Primary Rejection]
[Export] IDATT.WindowsService.JobFactory (ContractName="Quartz.Spi.IJobFactory")
[Import] IDATT.WindowsService.JobFactory.SaferWatchProcessor (ContractName="IDATT.WindowsService.Jobs.SaferWatchProcessor")
[Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint:
ContractName IDATT.WindowsService.Jobs.SaferWatchProcessor
RequiredTypeIdentity IDATT.WindowsService.Jobs.SaferWatchProcessor
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition)
at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id)
解决方法:
好吧,我认真思考.我认为发布我的发现会很有用.
发生的事情是我从NuGet安装了MEF 2,新的轻量级MEF用于4.5和Windows商店.
我现有的所有库都使用常规的旧MEF构建
使用System.ComponentModel.Composition,对于这个有问题的导入,ReSharper建议使用几个命名空间,我使用了System.Composition for Export属性导入
不确定MEF 2有什么用处,但它不适用于现有的MEF /出口库,所以要小心!
内容总结
以上是互联网集市为您收集整理的c# – 使用特定接口时的MEF组合导入问题全部内容,希望文章能够帮你解决c# – 使用特定接口时的MEF组合导入问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。