protected virtual void OnPluginExecuting(PluginExecutingEventArgs e)
 {
     if (this.EventsEnabled)
     {
         var evnt = this.PluginExecuting;
         if (evnt != null)
         {
             evnt(this, e);
         }
     }
 }
Esempio n. 2
0
 protected virtual void OnPluginExecuting(PluginExecutingEventArgs e)
 {
     if (this.EventsEnabled)
     {
         var evnt = this.PluginExecuting;
         if (evnt != null)
             evnt(this, e);
     }
 }
Esempio n. 3
0
        public virtual void Load(string pluginFileName, PluginParameters args)
        {
            if (args == null)
                args = new PluginParameters();

            AppDomainSetup domainSetup = new AppDomainSetup();
            Assembly entryAsm = Assembly.GetEntryAssembly();
            AssemblyProductAttribute[] atts = ((AssemblyProductAttribute[])entryAsm.GetCustomAttributes(typeof(AssemblyProductAttribute), false));
            string productName = null;
            if (atts != null)
            {
                if (atts.Length > 0)
                    productName = atts[0].Product;
            }

            if (String.IsNullOrEmpty(productName))
                domainSetup.ApplicationName = Path.GetFileNameWithoutExtension(entryAsm.Location);
            else
                domainSetup.ApplicationName = productName;

            domainSetup.ConfigurationFile = Path.GetFileName(pluginFileName) + ".config";
            domainSetup.ApplicationBase = Path.GetDirectoryName(pluginFileName);
            domainSetup.PrivateBinPath = "bin";

            if (this.ShadowCopyEnabled)
            {
                domainSetup.ShadowCopyFiles = "true";
                domainSetup.ShadowCopyDirectories = domainSetup.ApplicationBase + ";" + Path.Combine(domainSetup.ApplicationBase, "bin");
            }

            PluginLoadingEventArgs loadingArgs = new PluginLoadingEventArgs(pluginFileName, domainSetup);
            this.OnPluginLoading(loadingArgs);

            if (!loadingArgs.Cancel)
            {
                AppDomain domain = null;

                domain = AppDomain.CreateDomain(loadingArgs.DomainSetup.ApplicationName, null, loadingArgs.DomainSetup);
                Type t = this.GetType();
                PluginLoaderBase loader = (PluginLoaderBase)domain.CreateInstanceFromAndUnwrap(t.Assembly.Location, t.FullName);

                loader.AssemblyResolutionPaths = this.AssemblyResolutionPaths;
                PluginInfo info = loader.OnLoadWrapper(pluginFileName);

                if (info == null)
                    AppDomain.Unload(domain);
                else
                {
                    if (loader.IsPluginLoaded)
                    {
                        info.Domain = domain;
                        this._currentInfoList.Add(info);

                        PluginLoadedEventArgs pluginLoadedArgs = new PluginLoadedEventArgs(info);
                        this.OnPluginLoaded(pluginLoadedArgs);

                        PluginBase.ExecutionModes execMode = info.ExecutionMode;
                        bool canExecute = false;

                        if (!this.UseDefaultExecutionValidation)
                            canExecute = true;
                        else if (execMode == PluginBase.ExecutionModes.MultiInstance)
                            canExecute = true;
                        else if (execMode == PluginBase.ExecutionModes.Exclusive)
                        {
                            if (this._currentInfoList.Count == 1)
                                canExecute = true;
                        }
                        else if (execMode == PluginBase.ExecutionModes.Singleton)
                        {
                            if (this.GetLoadedInstances(info.PluginFileName).Count == 1)
                                canExecute = true;
                        }
                        else if (execMode == PluginBase.ExecutionModes.Custom)
                            canExecute = true;

                        if (canExecute)
                        {
                            info.Parameters = args;
                            PluginExecutingEventArgs exeArgs = new PluginExecutingEventArgs(info);
                            this.OnPluginExecuting(exeArgs);

                            if (exeArgs.Cancel)
                                this.Unload(info, args, true, exeArgs.CancelReason);
                            else
                            {
                                info.Loader = loader;

                                Thread pluginThread = new Thread(this.Execute);
                                pluginThread.IsBackground = true;
                                pluginThread.Start(info);
                            }
                        }
                        else
                            this.Unload(info, args, true, null);
                    }
                    else
                        AppDomain.Unload(domain);
                }
            }
        }
        public virtual void Load(string pluginFileName, PluginParameters args)
        {
            if (args == null)
            {
                args = new PluginParameters();
            }

            AppDomainSetup domainSetup = new AppDomainSetup();
            Assembly       entryAsm    = Assembly.GetEntryAssembly();

            AssemblyProductAttribute[] atts = ((AssemblyProductAttribute[])entryAsm.GetCustomAttributes(typeof(AssemblyProductAttribute), false));
            string productName = null;

            if (atts != null)
            {
                if (atts.Length > 0)
                {
                    productName = atts[0].Product;
                }
            }

            if (String.IsNullOrEmpty(productName))
            {
                domainSetup.ApplicationName = Path.GetFileNameWithoutExtension(entryAsm.Location);
            }
            else
            {
                domainSetup.ApplicationName = productName;
            }

            domainSetup.ConfigurationFile = Path.GetFileName(pluginFileName) + ".config";
            domainSetup.ApplicationBase   = Path.GetDirectoryName(pluginFileName);
            domainSetup.PrivateBinPath    = "bin";

            if (this.ShadowCopyEnabled)
            {
                domainSetup.ShadowCopyFiles       = "true";
                domainSetup.ShadowCopyDirectories = domainSetup.ApplicationBase + ";" + Path.Combine(domainSetup.ApplicationBase, "bin");
            }

            PluginLoadingEventArgs loadingArgs = new PluginLoadingEventArgs(pluginFileName, domainSetup);

            this.OnPluginLoading(loadingArgs);

            if (!loadingArgs.Cancel)
            {
                AppDomain domain = null;

                domain = AppDomain.CreateDomain(loadingArgs.DomainSetup.ApplicationName, null, loadingArgs.DomainSetup);
                Type             t      = this.GetType();
                PluginLoaderBase loader = (PluginLoaderBase)domain.CreateInstanceFromAndUnwrap(t.Assembly.Location, t.FullName);

                loader.AssemblyResolutionPaths = this.AssemblyResolutionPaths;
                PluginInfo info = loader.OnLoadWrapper(pluginFileName);

                if (info == null)
                {
                    AppDomain.Unload(domain);
                }
                else
                {
                    if (loader.IsPluginLoaded)
                    {
                        info.Domain = domain;
                        this._currentInfoList.Add(info);

                        PluginLoadedEventArgs pluginLoadedArgs = new PluginLoadedEventArgs(info);
                        this.OnPluginLoaded(pluginLoadedArgs);

                        PluginBase.ExecutionModes execMode = info.ExecutionMode;
                        bool canExecute = false;

                        if (!this.UseDefaultExecutionValidation)
                        {
                            canExecute = true;
                        }
                        else if (execMode == PluginBase.ExecutionModes.MultiInstance)
                        {
                            canExecute = true;
                        }
                        else if (execMode == PluginBase.ExecutionModes.Exclusive)
                        {
                            if (this._currentInfoList.Count == 1)
                            {
                                canExecute = true;
                            }
                        }
                        else if (execMode == PluginBase.ExecutionModes.Singleton)
                        {
                            if (this.GetLoadedInstances(info.PluginFileName).Count == 1)
                            {
                                canExecute = true;
                            }
                        }
                        else if (execMode == PluginBase.ExecutionModes.Custom)
                        {
                            canExecute = true;
                        }

                        if (canExecute)
                        {
                            info.Parameters = args;
                            PluginExecutingEventArgs exeArgs = new PluginExecutingEventArgs(info);
                            this.OnPluginExecuting(exeArgs);

                            if (exeArgs.Cancel)
                            {
                                this.Unload(info, args, true, exeArgs.CancelReason);
                            }
                            else
                            {
                                info.Loader = loader;

                                Thread pluginThread = new Thread(this.Execute);
                                pluginThread.IsBackground = true;
                                pluginThread.Start(info);
                            }
                        }
                        else
                        {
                            this.Unload(info, args, true, null);
                        }
                    }
                    else
                    {
                        AppDomain.Unload(domain);
                    }
                }
            }
        }