C# 在企业库中使用MEF
我正在尝试覆盖企业库的默认Unity容器行为,以便可以使用我的MEF容器。有一些资源解释了如何做到这一点,但我不明白:C# 在企业库中使用MEF,c#,mef,enterprise-library,C#,Mef,Enterprise Library,我正在尝试覆盖企业库的默认Unity容器行为,以便可以使用我的MEF容器。有一些资源解释了如何做到这一点,但我不明白: SO上也有这篇文章,但是代码没有编译,因为LogWriter受到保护。我认为这是指一个旧版本: 我的理解是,我需要为我的MEF容器使用CommonServiceLocator,然后将其附加到企业库容器。以下是我的容器配置器的配置: public class MefContainerConfigurator : IContainerConfigurator, IS
LogWriter
受到保护。我认为这是指一个旧版本:
CommonServiceLocator
,然后将其附加到企业库容器。以下是我的容器配置器的配置:
public class MefContainerConfigurator : IContainerConfigurator, IServiceLocator
{
[Import] private CatalogExportProvider provider;
public object GetService(Type serviceType)
{
throw new NotImplementedException();
}
public object GetInstance(Type serviceType)
{
return provider.GetExportedValue<Type>();
}
public object GetInstance(Type serviceType, string key)
{
throw new NotImplementedException();
}
public IEnumerable<object> GetAllInstances(Type serviceType)
{
throw new NotImplementedException();
}
public TService GetInstance<TService>()
{
return provider.GetExportedValue<TService>();
}
public TService GetInstance<TService>(string key)
{
return provider.GetExportedValue<TService>(key);
}
public IEnumerable<TService> GetAllInstances<TService>()
{
return provider.GetExportedValues<TService>();
}
public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
{
throw new NotImplementedException();
}
}
我认为可能需要使用LogWriterImpl
和ExceptionManagerImpl
类,因为这些类具有接受配置的构造函数。在这一点上,我的问题是:
- 如何从
检索配置,并将其提供给IConfigurationSource
和LogWriterImpl
构造函数ExceptionManagerImpl
调用myEnterpriseLibraryContainer.ConfigureContainer
中的MEFContainerConfiguration
。这是我应该在容器中注册所有企业库类型的地方吗Registeral
- 来自
接口的方法,我没有实现;我找不到使用这些从容器返回对象的方法。我是否应该让它们保持未实现状态,而改用泛型方法IServiceLocator
mefcontainerConfiguration
中编写了我的RegisterAll
方法来迭代类型注册并将它们添加到容器中。我一辈子都不知道如何将它们合并到我的引导程序类中创建的聚合容器中,以便我可以在容器配置程序之外实际使用这些导出:
public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
{
var registrations = rootProvider.GetRegistrations(configurationSource);
foreach (var type in registrations)
{
var builder = new RegistrationBuilder();
builder.ForType(type.ServiceType).Export();
var cat = new AssemblyCatalog(type.ServiceType.Assembly, builder);
var container = new CompositionContainer(cat);
container.ComposeParts(this);
}
}
在Prism引导程序中配置AggregateCatalog:
var configurator = new MefContainerConfigurator();
// Does this line read the Enterprise Library configuration from the app.config?
IConfigurationSource cs = new SystemConfigurationSource();
EnterpriseLibraryContainer.ConfigureContainer(configurator, cs);
protected override void ConfigureAggregateCatalog()
{
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
// Module assemblies
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(DataEntryModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ReportingModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StatusBarModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(SplashScreenModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(WelcomeModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(AdministrationModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
}
您的容器配置程序非常不完整。您有一个方法要实现:RegisterAll,但您没有实现它
您不必从配置中读取原始配置信息;相反,当您调用EnterpriseLibraryContianer.ConfigureContainer时,Entlib将在配置源中旋转,挑选出所有重要的位,并将一系列类型注册对象交给您。这些家伙给你类型映射,构造函数依赖,等等。基本上,你需要在你的容器中注册依赖的所有东西(在这个例子中,是MEF)
因此,基本上您需要做的是编写代码,将抽象TypeRegistration对象转换为MEF的正确配置
团结在这方面并不特别;它还有一个配置程序,并且遵循完全相同的过程,因此您可以在entlib代码中查看需要执行的各种操作的示例
我根本不知道MEF API,因此很遗憾,无法帮助您实现具体的实现。谢谢,这为我指明了Registeral方法的正确方向。请参阅我的编辑以了解修改后的问题,尽管我不认为您能够提供帮助,因为它现在是MEF特定的。