【问题标题】:run all modules of a catalog (prism)运行目录的所有模块(棱镜)
【发布时间】:2010-09-27 15:41:11
【问题描述】:

我正在使用 prism 的桌面库。

我想要的是在一个目录中获取模块,然后运行它们。

我喜欢这样:

DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";

我检查了,模块已加载到目录中。 模块示例:

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

然后我想运行它们(启动它们的 Initialize()),但我没有找到它。我想运行整个目录。有人有想法吗?我尝试了 catalog.Initialize()、catalog.Validate() 或 catalog.Load()

【问题讨论】:

    标签: wpf module prism modularity


    【解决方案1】:

    您的代码看起来正确,我的印象是您必须覆盖 Bootstrapper 类中的 GetModuleCatalog() 方法才能做到这一点。这是一个非常简单的 Bootstrapper 的工作示例,它从模块目录加载模块。

    public class Bootstrapper : UnityBootstrapper
    {
        private const string MODULE_FOLDER = @".\modules";
    
        protected override IModuleCatalog GetModuleCatalog()
        {
            DirectoryModuleCatalog catalog = new DirectoryModuleCatalog() { ModulePath = MODULE_FOLDER };
            return catalog;
        }
    }
    

    更新

    可能不使用引导程序并加载您的模块,但我不明白您为什么不利用 UnityBootstrapper 类,它为您工作。

    Bootstrapper bootStrapper = new Bootstrapper();
    bootStrapper.Run();
    

    您的模块将被加载。我自己从来没有在不使用引导程序的情况下这样做,因为它非常简单。

    【讨论】:

    • 是否可以不使用引导程序?实际上我不使用任何 shell,我将在 windows 服务中使用它(无 gui)。
    【解决方案2】:

    正如 jsmith 所说,默认引导程序为您自己执行的配置提供了繁重的工作。如果您没有 Shell,只需创建一个跳过该部分的自定义引导程序。

    如果您不想以任何方式使用引导程序,您可以只实例化 ModuleManager 类,将 ModulesCatalog 作为参数之一传递并调用 ModuleManager 的 Run 方法。

    正如我之前所说,以上是由引导程序为您完成的。 我希望这会有所帮助。

    谢谢, 达米安

    【讨论】:

    • 感谢您的回答。但是,如果我想每 15 分钟加载一次模块(使用计时器),我想我不能使用引导程序,因为它只在应用程序启动时加载,对吧?
    • @raphael:模块不应该被多次加载。它们只需要加载一次(就像在 appdomain 中加载程序集一样),它们的组件可以被重用。更多信息在这里:compositewpf.codeplex.com/Thread/View.aspx?ThreadId=59827compositewpf.codeplex.com/Thread/View.aspx?ThreadId=59040
    • 好的!所以我很好理解:如果我在我的目录“模块”中添加一个新模块(dll),我需要重新启动应用程序以考虑这个新模块?
    • 不一定。如果您想在应用程序运行时加载模块,您可以使用按需加载模块 (msdn.microsoft.com/en-us/library/ff921071%28PandP.20%29.aspx)。使用 FileSystemWatcher 并在目录中注册模块应该可以让您实现目标。 Prism 默认不提供此功能(监视目录并在目录更改时自动加载模块),但是对目录目录模块加载器进行一些更改应该没问题。
    【解决方案3】:

    您如何引导您的应用程序?您是否创建了引导程序类?它是初始化所有模块的引导程序。

    这是我创建的引导程序类的示例。

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

    然后在您的App.xaml 文件的OnStartup 中,您运行您的引导程序,它将在您的所有模块上调用初始化。

    protected override void OnStartup(StartupEventArgs e)
    {
        ApplicationBootstrapper bootstrapper = new ApplicationBootstrapper();
        bootstrapper.Run();
    }
    

    本示例使用 Unity 引导程序,您只需要 Unity platform 即可与 Prism 并行使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 2012-01-05
      • 2022-07-13
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多