WCF服务中的棱镜模块系统?

And*_*yuk 7 .net c# wcf prism mef

你能从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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Prism boostrapper,带有MEF风味:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的模块,其中包含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 ...
}
Run Code Online (Sandbox Code Playgroud)

最后这里是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 ...
}
Run Code Online (Sandbox Code Playgroud)

在过去的几个小时里尝试过这个并没有成功.我的db导入总是null永远不会被初始化.

请注意,如果我在没有Prism的情况下完成所有这些操作,那么一切都有效,但只能使用MEF.

And*_*yuk 0

好吧,看来解决方案根本就是不使用 Prism,因为它没有在其模块中添加任何“模块化”内容。看起来这些模块只是纯粹用于视觉应用程序的概念。

相反,必须挂钩 WCF“启动”过程并从那里引导 MEF 容器。关于如何做到这一点的答案相当复杂(尽管并不复杂),因为 WCF 已经有许多扩展/挂钩点。

我使用的答案位于Mark Seemann 所著的《.NET 中的依赖注入》一书中的第 7.3 章:“组合 WCF 应用程序”。

如果没有将该书的整个章节复制到这个答案中,恐怕这是我能做的最好的事情。