你能从 WCF 服务中提升一个 Prism 模块系统吗?因为无论我做什么,我的 MEF 依赖项都没有得到满足。
例如:
这是我的WCF 服务实现
public class MyService : IMyServiceContract{
// This should get filled by MEF after Prism loads the required modules
[Import]
IDatabase db;
public MyService(){
var bootsrapper = new MyServiceBoostrapper();
bootsrapper.Run();
}
}
这是我的具有 MEF 风格的Prism 助推器:
public class MyServiceBoostrapper : MefBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new ConfigurationModuleCatalog();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
// TODO: Add this assembly ... don't know why
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MyServiceBoostrapper).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(IDatabase).Assembly));
// This is what provides the service
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(DatabaseImpl).Assembly));
}
protected override DependencyObject CreateShell()
{
// we don't need the shell
return null;
}
}
这是我的模块,其中包含Database Prism 服务的接口:
[ModuleExport(typeof(IDatabase))]
public class ModuleActivator : IModule
{
public void Initialize()
{
// Do nothing as this module simply provides the API.
}
}
public interface IDatabase
{
// interface methods here ...
}
最后是Prism 数据库服务本身:
[ModuleExport(typeof(DatabaseImpl), DependsOnModuleNames = new string[] { "IDatabase" })]
public class ModuleActivator : IModule
{
public void Initialize()
{
// Do nothing as this is a library module.
}
}
[Export(typeof(IDatabase))]
public class DatabaseImpl : IDatabase
{
/// implementation here ...
}
在过去的几个小时里尝试了这个,但没有成功。我的db
导入总是null
并且永远不会初始化。
请注意,如果我在没有 Prism 的情况下执行所有这些操作,但只能使用 MEF,则一切正常。