예제 #1
0
        private IEnumerable <PluginContainer> loadPlugins(PluginAssemblyContainer assemblyLoadInfo)
        {
            if (assemblyLoadInfo.IsLoaded)
            {
                List <PluginContainer> plugins = new List <PluginContainer>();

                foreach (Type type in assemblyLoadInfo.CompilationAssembly.GetTypes().Where(t => t.Name.EndsWith("Plugin")))
                {
                    object    instance            = null;
                    Exception pluginLoadException = null;

                    try
                    {
                        if (assemblyLoadInfo.IsCodeFileValid())
                        {
                            instance = Activator.CreateInstance(type);
                        }
                    }
                    catch (Exception ex)
                    {
                        pluginLoadException = ex;
                    }

                    if (pluginLoadException == null)
                    {
                        PluginContainer pluginContainer = new PluginContainer(assemblyLoadInfo, instance);

                        plugins.Add(pluginContainer);

                        if (AutoInitializePlugins)
                        {
                            pluginContainer.Initialize();
                        }
                    }
                    else
                    {
                        plugins.Add(new PluginContainer(assemblyLoadInfo, pluginLoadException));
                    }
                }

                return(plugins);
            }

            return(new [] { new PluginContainer(assemblyLoadInfo, assemblyLoadInfo.CompilationException) });
        }
예제 #2
0
        public PluginContainer LoadPlugin(string virtualPath)
        {
            PluginAssemblyContainer assembly = compileAssembly(virtualPath, pluginAssemblies.Where(pa => string.Compare(pa.VirtualPath, virtualPath, true) == 0).FirstOrDefault());

            if (!assembly.IsLoaded)
            {
                return(null);
            }

            PluginContainer plugin = loadPlugins(assembly).FirstOrDefault();

            if (plugin != null)
            {
                return(plugin);
            }

            return(null);
        }
예제 #3
0
        public PluginContainer ReloadPlugin(Func <PluginContainer, bool> findPlugin, Func <PluginContainer, string> getVirtualPath)
        {
            lock (plugins)
            {
                PluginContainer foundPlugin = plugins.FirstOrDefault(findPlugin);
                PluginContainer pluginToAdd;

                if (foundPlugin != null)
                {
                    //TODO: (erikpo) Check the file date to make sure the plugin even needs reloaded

                    string virtualPath = getVirtualPath(foundPlugin);

                    if (!string.IsNullOrEmpty(virtualPath))
                    {
                        List <PluginContainer>  pluginsToRemove         = plugins.Where(p => string.Compare(p.VirtualPath, virtualPath, true) == 0).ToList();
                        PluginAssemblyContainer pluginAssemblyContainer = compileAssembly(virtualPath, foundPlugin);

                        pluginsToRemove.ForEach(p => plugins.Remove(p));
                        pluginAssemblies.Remove(pluginAssemblies.First(pa => string.Compare(pa.VirtualPath, virtualPath, true) == 0));

                        pluginAssemblies.Add(pluginAssemblyContainer);

                        pluginToAdd = loadPlugins(pluginAssemblyContainer).FirstOrDefault();
                    }
                    else
                    {
                        plugins.Remove(foundPlugin);

                        pluginToAdd = loadPlugins(foundPlugin).FirstOrDefault();
                    }

                    if (pluginToAdd != null)
                    {
                        plugins.Add(pluginToAdd);

                        return(pluginToAdd);
                    }
                }

                return(null);
            }
        }
예제 #4
0
        private PluginAssemblyContainer compileAssembly(string virtualPath, PluginAssemblyContainer previousPluginAssemblyContainer)
        {
            if (previousPluginAssemblyContainer != null)
            {
                PluginAssemblyContainer pluginAssemblyContainer = compileAssembly(virtualPath);
                PluginTimeout           timeout = new PluginTimeout(TimeSpan.FromSeconds(5));

                while (previousPluginAssemblyContainer.Equals(pluginAssemblyContainer))
                {
                    if (timeout.Expired)
                    {
                        return(new PluginAssemblyContainer(virtualPath, new TimeoutException(string.Format("Timeout expired while attempting to recompile '{0}'", virtualPath))));
                    }

                    Thread.Sleep(10);

                    pluginAssemblyContainer = compileAssembly(virtualPath);
                }

                return(pluginAssemblyContainer);
            }

            return(compileAssembly(virtualPath));
        }
예제 #5
0
 public PluginContainer(PluginAssemblyContainer assemblyContainer, object instance)
     : base(assemblyContainer.VirtualPath, assemblyContainer.CompilationAssembly)
 {
     Instance = instance;
 }
예제 #6
0
 public PluginContainer(PluginAssemblyContainer assemblyContainer, Exception pluginLoadException)
     : base(assemblyContainer.VirtualPath, pluginLoadException)
 {
 }