使用 RegistrationBuilder 的 MEF 导出不起作用
MEF Exporting using RegistrationBuilder does not work
我正在尝试使用 [Export] 属性和注册生成器填充 MEF 目录。
但是使用属性导出的服务可以解析,使用RegistrationBuilder注册的服务无法解析。
看看下面我的代码:
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 { }
我做错了什么?有人可以帮我吗?谢谢!
尝试
builder.ForType<Service2>().Export<IService2>();
没有
builder.ForType<IService2>().Export<Service2>();
我正在尝试使用 [Export] 属性和注册生成器填充 MEF 目录。 但是使用属性导出的服务可以解析,使用RegistrationBuilder注册的服务无法解析。 看看下面我的代码:
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 { }
我做错了什么?有人可以帮我吗?谢谢!
尝试
builder.ForType<Service2>().Export<IService2>();
没有
builder.ForType<IService2>().Export<Service2>();