Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism module system from within WCF service?

Tags:

c#

.net

wcf

mef

prism

Can you boostrap a Prism module system from within the WCF service? Because no matter what I do my MEF dependencies are not being fulfilled.

E.g.:

This is my WCF service implementation

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

This is my Prism boostrapper with MEF flavor:

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

}

Here is my module that contains the interfaces for Database Prism service :

[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 ...
}

and lastly here is the Prism database service itself:

[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 ...
}

Tried this for the last few hours with no success. My db import is always null and is never initialized.

Note, that everything works if I do all of this without Prism, but only with MEF.

like image 787
Andriy Drozdyuk Avatar asked May 24 '13 19:05

Andriy Drozdyuk


1 Answers

You won't have anything imported to your db field because the MyService object is not created by the container - it can't be created by it because the container is actually being created in the bootstrapper, which is in MyService's constructor.

One simple way to solve this is to satisfy your object's imports after the container is initialized. To do so, you can expose the container in the bootstrapper like so:

public class MyServiceBoostrapper
{
    public CompositionContainer MyServiceContainer
    {
        get { return Container; }
    }

    // Rest of bootstrapper definitions...
}

Then modify MyService's constructor:

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.
}
like image 195
Adi Lester Avatar answered Sep 27 '22 17:09

Adi Lester