[英]MEF Exporting using RegistrationBuilder does not work
I am trying to populate a MEF Catalog using [Export] Attributes and the Registration Builder.我正在尝试使用 [Export] 属性和注册生成器填充 MEF 目录。 However the service exported using attributes can be resolved, the one registered using the RegistrationBuilder cannot be resolved.
但是可以解析使用属性导出的服务,无法解析使用 RegistrationBuilder 注册的服务。 Have a look at my code below:
看看我下面的代码:
static class Program
{
[STAThread]
static void Main()
{
// export service 2 using registration builder
var builder = new RegistrationBuilder();
builder.ForType<IService2>().Export<Service2>();
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly(), builder);
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
container.SatisfyImportsOnce(builder);
// service exported using attribute can be resolved
var service1 = container.GetExport<IService1>();
// throws
var service2 = container.GetExport<IService2>();
}
}
// create two dummy services
public interface IService1 { }
public interface IService2 { }
// export service 1 with attribute
[Export(typeof(IService1))]
public class Service1 : IService1 { }
public class Service2 : IService2 { }
What am i doing wrong?我究竟做错了什么? Can somebody help me?
有人可以帮助我吗? Thanks!
谢谢!
Try尝试
builder.ForType<Service2>().Export<IService2>();
not不是
builder.ForType<IService2>().Export<Service2>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.