private PluginDescriptor LoadPlugin(string pluginAssemblyPath) { PluginDescriptorInternal pluginDescriptor = null; try { var setup = AppDomain.CurrentDomain.SetupInformation; setup.PrivateBinPath = PluginDirectory; var appDomain = AppDomain.CreateDomain(string.Format("Plugin_{0}", Guid.NewGuid()), null, setup); pluginDescriptor = new PluginDescriptorInternal(appDomain, null, null, pluginAssemblyPath); var handler = new PluginUnhandledExceptionHandler(pluginDescriptor); handler.UnhandledExceptionOccurred += (sender, args) => { OnUnhandledExceptionOccurred(args); }; var pluginWrapper = (PluginWrapper) appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(PluginWrapper).FullName); pluginWrapper.CreatePluginInstance(pluginAssemblyPath); pluginDescriptor.DataReaderPlugin = pluginWrapper; pluginDescriptor.FriendlyName = pluginWrapper.FriendlyName; pluginDescriptor.Id = pluginWrapper.Id; _descriptors[pluginWrapper.Id] = pluginDescriptor; return pluginDescriptor.ToPluginDescriptor(); } catch (Exception e) { if (pluginDescriptor != null) { UnloadPlugin(pluginDescriptor); } throw; } }
internal void UnloadPlugin(PluginDescriptorInternal pluginDescriptor) { try { UnloadPluginDomain(pluginDescriptor); } catch (CannotUnloadAppDomainException unloadAppDomainException) { throw new PluginLoadingException("Cannot unload the plugin.", unloadAppDomainException); } }
private void UnloadPluginDomain(PluginDescriptorInternal descriptor) { if (descriptor == null || descriptor.AppDomain == null) { return; } try { AppDomain.Unload(descriptor.AppDomain); } finally { descriptor.AppDomain = null; descriptor.DataReaderPlugin = null; } }
public PluginUnhandledExceptionHandler(PluginDescriptorInternal pluginDescriptor) { _pluginDescriptor = pluginDescriptor; _pluginDescriptor.AppDomain.UnhandledException += PluginAppDomainUnhandledException; }