Prics模块系统从WCF服务内部?
你能从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 ,并且从不初始化。 
请注意,如果我在没有棱镜的情况下完成所有这些工作,但只有MEF才能正常工作。
  你不会有任何导入到你的db字段,因为在MyService对象不是由容器产生的-它无法通过,因为实际上正在引导程序,这是创建的容器中创建MyService的构造。 
解决这个问题的一个简单方法是在容器初始化后满足对象的导入。 为此,您可以像下面这样在引导程序中公开容器:
public class MyServiceBoostrapper
{
    public CompositionContainer MyServiceContainer
    {
        get { return Container; }
    }
    // Rest of bootstrapper definitions...
}
  然后修改MyService的构造函数: 
public MyService()
{
    var bootsrapper = new MyServiceBoostrapper();
    bootsrapper.Run();
    // This is an extension method. You'll need to add
    // System.ComponentModel.Composition to your using statements.
    bootstrapper.MyServiceContainer.SatisfyImportsOnce(this);
    // At this stage, "db" should not be null.
}
我不确定以下代码段会对您有所帮助。 我只有PRISM和Unity的经验。 试试看,告诉我发生了什么事。
protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        this.RegisterTypeIfMissing(typeof(IDatabase), typeof(DatabaseImpl ), true);
    }
您还正在创建并清空ModuleCatalog,并且不会对其进行配置。
protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
            var moduleCatalog = (ModuleCatalog)ModuleCatalog;
            Type Initial = typeof(ModuleActivator);
            moduleCatalog.AddModule(new ModuleInfo
            {
                ModuleName = Initial.Name,
                ModuleType = Initial.AssemblyQualifiedName
            });
        }
那么,看起来解决方案根本就不使用Prism,因为它不会在模块中添加任何“模块化”的东西。 看起来这些模块纯粹是针对视觉应用的概念。
相反,我们必须挂钩WCF“启动”过程并从那里提升MEF容器。 如何做到这一点的答案是相当复杂的(虽然不复杂),因为WCF已经有很多扩展/挂钩点。
我使用的答案在于Mark Seemann在第7.3章“构建WCF应用程序”中的.NET依赖注入。
这本书的全部内容都没有复制到这个答案中,恐怕这是我能做的最好的。
链接地址: http://www.djcxy.com/p/71587.html上一篇: Prism module system from within WCF service?
下一篇: Called to stored procedure through VB.net application not working
