C# Exception during loading DLL's. Can't find solution

15,016

When you build your code in debug mode, two files are created,one the class library and other the 'debug database' .pdb file.if you are running your code in debug mode, include pdb file as well in bin of your reflection code. Another thing is to look at the Fusion logs using fuslogvw in command prompt.it may give any runtime failures/dependrncies that are not taken care of.

Share:
15,016
krzakov
Author by

krzakov

Java dev.

Updated on July 13, 2022

Comments

  • krzakov
    krzakov almost 2 years

    I have a problem, while loading DLL at runtime in C#.

    Following code:

    foreach (var f in Directory.EnumerateFiles(startFolder, "*.dll",            SearchOption.AllDirectories))
    {
       try
       {
           var assembly = Assembly.LoadFrom(f);
           var types = assembly.GetTypes(); //exception raised here
    
           foreach (var type in types)
           {
                if (type.GetInterface("IPlayerFactory") != null)
                     instances.Add((IPlayerFactory)Activator.CreateInstance(type));
           }
           assembly = null;
        }
        catch (ReflectionTypeLoadException ex) { /* later, keep reading  */}
    

    So exception says:

    An exception of type 'System.Reflection.ReflectionTypeLoadException' occurred in
    mscorlib.dll but was not handled in user code
    
    Additional information: Unable to load one or more of the requested types. Retrieve the    
    LoaderExceptions property for more information.
    

    I searched stack a little bit and found this catch to look for more information (source: here)

    catch block:

    catch (ReflectionTypeLoadException ex)
    {
         StringBuilder sb = new StringBuilder();
         foreach (Exception exSub in ex.LoaderExceptions)
         {
              sb.AppendLine(exSub.Message);
              FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
              if (exFileNotFound != null)
              {
                  if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                  {
                      sb.AppendLine("Fusion Log:");
                      sb.AppendLine(exFileNotFound.FusionLog);
                  }
              }
                        sb.AppendLine();
          }
          string errorMessage = sb.ToString();
          //Display or log the error based on your application.
          Console.WriteLine(sb);
     }
    

    So i managed to extract:

    'Battleships.Desktop.vshost.exe' (CLR v4.0.30319: Battleships.Desktop.vshost.exe):
     Loaded 'C:\SomePath\some_dll.dll'. Cannot find
     or open the PDB file.
     A first chance exception of type 'System.Reflection.ReflectionTypeLoadException'  
     occurred in mscorlib.dll
    

    I tried some solutions but nothing works. Any fresh ideas?

  • krzakov
    krzakov almost 10 years
    Well, currently i'm debugging it in DEBUG mode as You predicted. How should I add it and where?
  • krzakov
    krzakov almost 10 years
    I inspected `bin\Debug` directory and I can't see any PDB's in here.