Prism module module from WCF service?

Can you activate the Prism modular system from the WCF service? Because no matter what I do, my MEF dependencies are not fulfilled.

eg:.

This is my implementation of WCF service

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 MEF flavor prismatic accelerator :

 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 containing the interfaces for the Prism database 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 finally, this is the Prism database service :

 [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 without success. My db import is always null and never initialized.

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

+7
source share
3 answers

Well, it seems the solution is not to use Prism at all, since it does not add anything “modular” with its modules. The modules seem to be concepts exclusively for visual applications.

Instead, you need to connect to the WCF "WCF" startup procedure and activate the MEF container. The answer to the question of how to do this is more likely involved (although not difficult), since WCF already has many extension / hook points.

The answer I used lies in Mark Seemann’s Dependency Injection .NET book in Chapter 7.3: Compiling WCF Applications.

Without copying an entire chapter from this book into this answer, I am afraid that the best I can do.

0
source

Nothing will be imported from you into the db field, because the MyService object MyService not created by the container - it cannot be created by it, because the container is actually created in the loader, which is in the MyService constructor.

One easy way to solve this problem is to satisfy the import of objects after container initialization. To do this, you can open the container in the boot block as follows:

 public class MyServiceBoostrapper { public CompositionContainer MyServiceContainer { get { return Container; } } // Rest of bootstrapper definitions... } 

Then change the constructor of 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. } 
+3
source

I'm not sure the following snippets will help you. I have only experience with PRISM and Unity. Just try and tell me what is going on.

 protected override void ConfigureContainer() { base.ConfigureContainer(); this.RegisterTypeIfMissing(typeof(IDatabase), typeof(DatabaseImpl ), true); } 

You also create and release ModuleCatalog and never configure it.

 protected override void ConfigureModuleCatalog() { base.ConfigureModuleCatalog(); var moduleCatalog = (ModuleCatalog)ModuleCatalog; Type Initial = typeof(ModuleActivator); moduleCatalog.AddModule(new ModuleInfo { ModuleName = Initial.Name, ModuleType = Initial.AssemblyQualifiedName }); } 
+1
source

All Articles