public IPluginInteractionLink CreateInteractionLink(IPluginDescriptor pluginDescriptor, IPlugin pluginRawInstance, IPluginController pluginController, IPluginInstance pluginManagedInstance) { return new StandardPluginInteractionLink(pluginDescriptor, pluginRawInstance, pluginController, pluginManagedInstance); }
/* #endregion Public Constructors */ /* #region Public Methods */ public IEnumerable <IPluginInstance> CreatePluginInstances(Assembly assembly) { int count = 0; foreach (Type type in assembly.GetTypes()) { if (typeof(IPluginInstance).IsAssignableFrom(type)) { var ctors = type.GetConstructors(); var spCtor = ctors.FirstOrDefault(p => { var parms = p.GetParameters(); if (parms.Count() != 1) { return(false); } var parm = parms.First(); return(parm.ParameterType == typeof(IServiceProvider)); }); if (spCtor == null) { spCtor = ctors.FirstOrDefault(p => p.GetParameters().Count() == 0); } if (spCtor == null) { throw new Exception($"No valid constructor found on type {type}"); } IPluginInstance result = Activator.CreateInstance(type, this.ServiceProvider) as IPluginInstance; if (result != null) { count++; yield return(result); } } } if (count == 0) { string availableTypes = string.Join(",", assembly.GetTypes().Select(t => t.FullName)); throw new ApplicationException( $"Can't find any type which implements IPluginInstance in {assembly} from {assembly.Location}.\n" + $"Available types: {availableTypes}"); } }
public object CreatePluginInstance(IPluginDescriptor descriptor, string pluginAssemblyPath, IPluginInstance pluginManagedInstance, IPluginInteractionLinkFactory pluginInteractionLinkFactory) { if (descriptor == null) throw new ArgumentNullException("Plugin descriptor must be supplied"); if (string.IsNullOrEmpty(pluginAssemblyPath)) throw new ArgumentNullException("Plugin assembly path must be supplied"); try { lock (_lock) { if (!_pluginInstance.IsWritten) { _pluginDescriptor.Value = descriptor; _pluginInstance.Value = (IPlugin)_appDomainBridge.Value.CreateInstance(descriptor.PluginTypeName, pluginAssemblyPath); _pluginInteractionLink.Value = pluginInteractionLinkFactory.CreateInteractionLink( descriptor, _pluginInstance.Value, this, pluginManagedInstance); return _pluginInstance.Value; } else { throw new InvalidOperationException("Controller already created plugin instance"); } } } catch (Exception ex) { throw new ApplicationException("Failed to create plugin instance", ex); } }
protected override void DoUninit() { try { lock (_lock) { if (_processInstance != null) { _processInstance.UninitProcess(); } if (_pluginAssembly != null && _pluginAssembly.IsInitialised) { _pluginAssembly.Unitialise(); } _pluginAssembly = null; _pluginInstance = null; _processInstance = null; } } catch (Exception ex) { throw new ApplicationException("Failed to do uninit", ex); } }
protected override void DoInit() { try { lock (_lock) { Assembly.LoadFrom(_pluginDescriptor.AssemblyPath); AppDomain.CurrentDomain.AssemblyResolve += (s, e) => { return AppDomain.CurrentDomain.GetAssemblies() .DefaultIfEmpty(null) .SingleOrDefault(asm => asm.FullName == e.Name); }; try { _pluginAssembly = _pluginAssemblyFactory.CreatePluginAssemblyFromPath(_pluginDescriptor.AssemblyPath); var initRes = _pluginAssembly.Initialise(); if (!initRes.HasUsablePlugins) { throw new ApplicationException("The plugin assembly contains no usable plugins"); } if (initRes.UsablePlugins.DefaultIfEmpty(null) .SingleOrDefault(p => p.Match(_pluginDescriptor)) == null) { throw new ApplicationException("A matching plugin descriptor couldn't be found in the plugin assembly"); } } catch (Exception ex) { throw new ApplicationException("Failed to initialise plugin assembly and locate plugin", ex); } try { _pluginInstance = _pluginAssembly.CreatePluginInstance(_pluginDescriptor); _pluginInstance.Initialise(); } catch (Exception ex) { throw new ApplicationException("Failed to create and initialise the plugin instance", ex); } try { _processInstance = _pluginInstance.GetUnderlyingInstance<IProcess>(); _processInstance.InitProcess(); if (_processInstance.JobDefinitions == null || _processInstance.JobDefinitions.Count == 0) { throw new ApplicationException("Process doesn't have any job definitions"); } } catch (Exception ex) { throw new ApplicationException("Failed to create and initialise the process", ex); } } } catch (Exception ex) { throw new ApplicationException("Failed to perform init", ex); } }
public PluginWrapper(IPluginInstance instance) { this.pluginInstance = instance; this.ErrorPlugin = instance.Plugin as ErrorPlugin; }