Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4534


Ignore:
Timestamp:
09/28/10 16:22:40 (14 years ago)
Author:
gkronber
Message:

Implemented fix for #1220 (Plugin infrastructure throws a BadImageFormatException when x86 and x64 plugins are installed side by side.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginValidator.cs

    r4482 r4534  
    121121      CheckPluginDependencies(pluginDescriptions);
    122122
     123      // test full loading (in contrast to reflection only loading) of plugins
     124      // disables plugins that are not loaded correctly
     125      CheckExecutionContextLoad(pluginDescriptions);
     126
    123127      // mark all plugins as enabled that were not disabled in CheckPluginFiles, CheckPluginAssemblies,
    124       // CheckCircularDependencies, or CheckPluginDependencies
     128      // CheckCircularDependencies, CheckPluginDependencies and CheckExecutionContextLoad
    125129      foreach (var desc in pluginDescriptions)
    126130        if (desc.PluginState != PluginState.Disabled)
    127131          desc.Enable();
    128132
    129       // test full loading (in contrast to reflection only loading) of plugins
    130       // disables plugins that are not loaded correctly
     133      // load the enabled plugins
    131134      LoadPlugins(pluginDescriptions);
    132135
     
    490493    }
    491494
    492     private void LoadPlugins(IEnumerable<PluginDescription> pluginDescriptions) {
     495    // tries to load all plugin assemblies into the execution context
     496    // if an assembly of a plugin cannot be loaded the plugin is disabled
     497    private void CheckExecutionContextLoad(IEnumerable<PluginDescription> pluginDescriptions) {
    493498      // load all loadable plugins (all dependencies available) into the execution context
    494499      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(pluginDescriptions
    495500                                                                                .Where(x => x.PluginState != PluginState.Disabled))) {
    496         List<Type> types = new List<Type>();
    497501        foreach (string assemblyLocation in desc.AssemblyLocations) {
    498           // now load the assemblies into the execution context
    499           var asm = Assembly.LoadFrom(assemblyLocation);
    500           foreach (Type t in asm.GetTypes()) {
    501             if (typeof(IPlugin).IsAssignableFrom(t)) {
    502               types.Add(t);
     502          try {
     503            // now load the assemblies into the execution context 
     504            // this can still lead to an exception
     505            // even when the assembly was successfully loaded into the reflection only context before
     506            var asm = Assembly.LoadFrom(assemblyLocation);
     507          }
     508          catch (BadImageFormatException) {
     509            desc.Disable(Path.GetFileName(assemblyLocation) + " is not a valid assembly.");
     510          }
     511          catch (FileLoadException) {
     512            desc.Disable("Can't load file " + Path.GetFileName(assemblyLocation));
     513          }
     514          catch (FileNotFoundException) {
     515            desc.Disable("File " + Path.GetFileName(assemblyLocation) + " is missing.");
     516          }
     517          catch (SecurityException) {
     518            desc.Disable("File " + Path.GetFileName(assemblyLocation) + " can't be loaded because of security constraints.");
     519          }
     520        }
     521      }
     522    }
     523
     524    // assumes that all plugin assemblies have been loaded into the execution context via CheckExecutionContextLoad
     525    // for each enabled plugin:
     526    // calls OnLoad method of the plugin
     527    // and raises the PluginLoaded event
     528    private void LoadPlugins(IEnumerable<PluginDescription> pluginDescriptions) {
     529      List<Assembly> assemblies = new List<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
     530      foreach (var desc in pluginDescriptions) {
     531        if (desc.PluginState == PluginState.Enabled) {
     532          // cannot use ApplicationManager to retrieve types because it is not yet instantiated
     533          foreach (string assemblyLocation in desc.AssemblyLocations) {
     534            var asm = (from assembly in assemblies
     535                      where string.Equals(Path.GetFullPath(assembly.Location), Path.GetFullPath(assemblyLocation), StringComparison.CurrentCultureIgnoreCase)
     536                      select assembly)
     537                      .Single();
     538
     539            foreach (Type pluginType in asm.GetTypes()) {
     540              if (typeof(IPlugin).IsAssignableFrom(pluginType) && !pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
     541                IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
     542                plugin.OnLoad();
     543                OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
     544              }
    503545            }
    504           }
    505         }
    506 
    507         foreach (Type pluginType in types) {
    508           if (!pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
    509             IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
    510             plugin.OnLoad();
    511             OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
    512           }
    513         }
    514         desc.Load();
    515       }
     546          } // end foreach assembly in plugin
     547          desc.Load();
     548        }
     549      } // end foreach plugin description
    516550    }
    517551
Note: See TracChangeset for help on using the changeset viewer.