我有一个应用程序,其中我使用MEF进行组合,使用unity进行DI。在某种程度上,我有一个组件,它使用MEF为一个服务加载许多提供者。有些是否应该使用Unity实例化提供者,因为它们需要在实例化时接收add'l组件。
Unity需要创建provider,并将依赖类型/接口注册到容器中。
但是,当根组件通过MEF组成时(列表中填满了MEF注册的所有导出组件),需要使用Unity构建的组件不会被注入依赖项。
我的代码是
public class LoggerManager
{
[ImportMany(typeof(ILogger))]
private List<ILogger> _loggers { get; set; }
}而记录器是
[Export(typeof(ILogger))]
public class WinEventLog : EventLog, ILogger
{
private IWinLoggerConfigurator _config = null;
public WinEventLog(IWinLoggerConfigurator config)
{
_config=config;
}
}在我的主代码中,在尝试编写LogManager之前
UnityHelper.Container.RegisterType<IWinLoggerConfigurator , MyWinLoggerConfigurator>();如何才能做到这一点?
谢谢
发布于 2012-03-11 21:05:52
您还应该查看这个MEFContrib项目- http://mefcontrib.codeplex.com/wikipage?title=Unity%20Integration
发布于 2012-03-11 20:45:35
我找到了下面的帖子,描述了这种情况,并提出了解决方案。
http://pwlodek.blogspot.com/2009/05/unity-mef-integration-layer.html
https://stackoverflow.com/questions/9654791
复制相似问题