Prism module system from within WCF service?
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.
Eg:
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.
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.
}
I'm not sure that the following snippets will help you. I have only experiance with PRISM and Unity. Just try it and tell me what's happening.
protected override void ConfigureContainer()
{
base.ConfigureContainer();
this.RegisterTypeIfMissing(typeof(IDatabase), typeof(DatabaseImpl ), true);
}
You are also creating and empty 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
});
}
Well, it seems like the solution is not to use Prism at all, as it does not add anything "modular" with it's modules. It seems like the modules are concepts purely for visual applications.
Instead, one has to hook into the WCF "startup" procedure and boostrap the MEF container from there. The answer on how to do this is rather involved (though not complicated), as WCF already has many extension/hook-in points.
The answer I used lies in the book Dependency Injection in .NET by Mark Seemann in Chapter 7.3: "Composing WCF applications".
Short of copying the whole chapter from that book into this answer, I'm afraid that's the best I can do.
链接地址: http://www.djcxy.com/p/71588.html上一篇: 在IIS 7.5和.NET中超过2GB文件上传限制
下一篇: Prics模块系统从WCF服务内部?