7

你能从 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,则一切正常。

4

3 回答 3

3

您不会将任何内容导入您的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.
}
于 2013-06-01T21:01:29.823 回答
1

我不确定以下片段是否会对您有所帮助。我只有 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
            });
        }
于 2013-05-27T13:54:55.330 回答
0

好吧,似乎解决方案根本不使用 Prism,因为它没有在其模块中添加任何“模块化”。这些模块似乎是纯粹用于视觉应用的概念。

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

我使用的答案在于Mark Seemann 在第 7.3 章:“编写 WCF 应用程序”中的 .NET 中的依赖注入一书。

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

于 2013-06-03T03:40:46.230 回答