@ -17,6 +17,7 @@ namespace ImageProcessor.Web.Config
using System.Linq ;
using System.Linq ;
using System.Reflection ;
using System.Reflection ;
using System.Text ;
using System.Text ;
using System.Web.Compilation ;
using ImageProcessor.Processors ;
using ImageProcessor.Processors ;
#endregion
#endregion
@ -287,22 +288,21 @@ namespace ImageProcessor.Web.Config
private void LoadGraphicsProcessors ( )
private void LoadGraphicsProcessors ( )
{
{
if ( this . GraphicsProcessors = = null )
if ( this . GraphicsProcessors = = null )
{
try
{
{
if ( GetImageProcessingSection ( ) . Plugins . AutoLoadPlugins )
if ( GetImageProcessingSection ( ) . Plugins . AutoLoadPlugins )
{
{
// Build a list of native IGraphicsProcessor instances.
Type type = typeof ( IGraphicsProcessor ) ;
Type type = typeof ( IGraphicsProcessor ) ;
IEnumerable < Type > types =
try
AppDomain . CurrentDomain . GetAssemblies ( )
{
// Build a list of native IGraphicsProcessor instances.
List < Type > availableTypes = BuildManager . GetReferencedAssemblies ( )
. Cast < Assembly > ( )
. SelectMany ( s = > s . GetTypes ( ) )
. SelectMany ( s = > s . GetTypes ( ) )
. Where ( p = > type . IsAssignableFrom ( p ) & & p . IsClass & & ! p . IsAbstract )
. Where ( t = > t ! = null & & t ype . IsAssignableFrom ( t ) & & t . IsClass & & ! t . IsAbstract )
. ToList ( ) ;
. ToList ( ) ;
// Create them and add.
// Create them and add.
this . GraphicsProcessors =
this . GraphicsProcessors = availableTypes . Select ( x = > ( Activator . CreateInstance ( x ) as IGraphicsProcessor ) ) . ToList ( ) ;
types . Select ( x = > ( Activator . CreateInstance ( x ) as IGraphicsProcessor ) ) . ToList ( ) ;
// Add the available settings.
// Add the available settings.
foreach ( IGraphicsProcessor processor in this . GraphicsProcessors )
foreach ( IGraphicsProcessor processor in this . GraphicsProcessors )
@ -310,7 +310,25 @@ namespace ImageProcessor.Web.Config
processor . Settings = this . GetPluginSettings ( processor . GetType ( ) . Name ) ;
processor . Settings = this . GetPluginSettings ( processor . GetType ( ) . Name ) ;
}
}
}
}
catch ( ReflectionTypeLoadException ex )
{
this . LoadGraphicsProcessorsFromConfiguration ( ) ;
}
}
else
else
{
this . LoadGraphicsProcessorsFromConfiguration ( ) ;
}
}
}
/// <summary>
/// Loads graphics processors from configuration.
/// </summary>
/// <exception cref="TypeLoadException">
/// Thrown when an <see cref="IGraphicsProcessor"/> cannot be loaded.
/// </exception>
private void LoadGraphicsProcessorsFromConfiguration ( )
{
{
ImageProcessingSection . PluginElementCollection pluginConfigs = imageProcessingSection . Plugins ;
ImageProcessingSection . PluginElementCollection pluginConfigs = imageProcessingSection . Plugins ;
this . GraphicsProcessors = new List < IGraphicsProcessor > ( ) ;
this . GraphicsProcessors = new List < IGraphicsProcessor > ( ) ;
@ -320,7 +338,7 @@ namespace ImageProcessor.Web.Config
if ( type = = null )
if ( type = = null )
{
{
throw new Argument Exception( "Couldn't load IGraphicsProcessor: " + pluginConfig . Type ) ;
throw new TypeLoad Exception( "Couldn't load IGraphicsProcessor: " + pluginConfig . Type ) ;
}
}
this . GraphicsProcessors . Add ( Activator . CreateInstance ( type ) as IGraphicsProcessor ) ;
this . GraphicsProcessors . Add ( Activator . CreateInstance ( type ) as IGraphicsProcessor ) ;
@ -332,33 +350,6 @@ namespace ImageProcessor.Web.Config
processor . Settings = this . GetPluginSettings ( processor . GetType ( ) . Name ) ;
processor . Settings = this . GetPluginSettings ( processor . GetType ( ) . Name ) ;
}
}
}
}
}
catch ( ReflectionTypeLoadException ex )
{
StringBuilder stringBuilder = new StringBuilder ( ) ;
foreach ( Exception exception in ex . LoaderExceptions )
{
stringBuilder . AppendLine ( exception . Message ) ;
if ( exception is FileNotFoundException )
{
FileNotFoundException fileNotFoundException = exception as FileNotFoundException ;
if ( ! string . IsNullOrEmpty ( fileNotFoundException . FusionLog ) )
{
stringBuilder . AppendLine ( "Fusion Log:" ) ;
stringBuilder . AppendLine ( fileNotFoundException . FusionLog ) ;
}
}
stringBuilder . AppendLine ( ) ;
}
string errorMessage = stringBuilder . ToString ( ) ;
// Display or log the error based on your application.
throw new Exception ( errorMessage ) ;
}
}
}
#endregion
#endregion
}
}
}
}