i'm using the desktop library of prism.
what i want is to get modules in a directory and then, run them.
I do like that:
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";
I checked, the modules are loaded in the catalog. Example of a module:
public class SendEmailClass : IModule
{
public void SendEmail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("**", "moi");
mail.Subject = "Report"; //manage generated subject
mail.To.Add("***");
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
开发者_运维问答 smtp.Port = 57;
smtp.EnableSsl = true; //depending of the smtp server
NetworkCredential cred = new NetworkCredential("***", "***");
smtp.Credentials = cred;
smtp.Send(mail);
}
public void Initialize()
{
SendEmail();
}
}
But then i want to run them (launch their Initialize()) but i don't find it. I want to run the whole catalog. someone has an idea ? I tried catalog.Initialize(), catalog.Validate() or catalog.Load()
Your code looks correct, I was under the impression that you had to override the GetModuleCatalog()
method in your Bootstrapper
class in order to do this. Here is a working example of a pretty straight forward Bootstrapper
that loads modules from a modules directory.
public class Bootstrapper : UnityBootstrapper
{
private const string MODULE_FOLDER = @".\modules";
protected override IModuleCatalog GetModuleCatalog()
{
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog() { ModulePath = MODULE_FOLDER };
return catalog;
}
}
Update
It is probably possible to not use a bootstrapper and load your modules, but I do not see why you would not take advantage of the UnityBootstrapper
class, it does the work for you.
Bootstrapper bootStrapper = new Bootstrapper();
bootStrapper.Run();
Your modules will be loaded. I myself have never done this without using the bootstrapper because it is very straightforward.
As jsmith said, the default bootstrapper provides the heavy lifting for the configuration that you would otherwise perform yourself. If you don't have a Shell, simply create a custom bootstrapper that skips over that part.
In case you don't want to use a bootstrapper by any means, you can just instantiate the ModuleManager class, passing the ModulesCatalog as one of the parameters and call the ModuleManager's Run method.
As I said before, the above is done by the bootstrapper for you. I hope this helps.
Thanks, Damian
How are you bootstrapping your application? Have you created a bootstrapper class? It is the bootstrapper that initializes all of your modules.
This is an example of a bootstrapper class I made.
public class ApplicationBootstrapper : UnityBootstrapper
{
// Here is where you create your module catalog
protected override IModuleCatalog GetModuleCatalog()
{
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";
return catalog;
}
// Here is where you create your user interface shell.
protected override DependencyObject CreateShell()
{
Container.RegisterInstance<IApplicationSettings>(new ApplicationSettings());
Shell shell = Container.Resolve<Shell>();
if (shell != null)
shell.Show();
return shell;
}
}
Then in your App.xaml
file's OnStartup
, you run your bootstrapper and it will call initialize on all of your modules.
protected override void OnStartup(StartupEventArgs e)
{
ApplicationBootstrapper bootstrapper = new ApplicationBootstrapper();
bootstrapper.Run();
}
This example uses the Unity bootstrapper, all you need is the Unity platform which is available side-by-side with Prism.
精彩评论