Unable to load one or more of the requested types

by David Kiff 21. September 2009 16:41

I have spent around 4 hours trying to diagnose the follow exception:

“Unable to load one or more of the requested types”

This error message is extremely unhelpful, so I took a look at the stack, that suggested that EntityFramework was causing the exception:

 

Retrieve the LoaderExceptions property for more information. at System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark) at System.Reflection.Assembly.GetTypes() at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadTypesFromAssembly(LoadingContext context) at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.InternalLoadAssemblyFromCache(LoadingContext context)

 

The stack has contains a helpful message that I overlooked in my haste to fix the problem “Retrieve the LoaderExceptions property for more information”. 

The next question is how do you retrieve “more information”?  Where is that property?  The answer lies within the ReflectionTypeLoadException, which contains the LoaderExceptions property (an array of helpful exceptions).

In the debugger it would have been easy to identify this, however this issue was appearing in our test environment with no debugging support.  I ended up adding a try and catch around the offending code (found from the original stack trace), then casting the exception to ReflectionTypeLoadException and persisting the message and stack trace to a file:

try
{
        //Do work
}

catch(Exception ex)
{         

             ReflectionTypeLoadException exception = ex as ReflectionTypeLoadException;

              if(exception == null)
                  System.IO.File.AppendAllText("C:\\TestLog.txt", "Not a ReflectionTypeLoadException ex.");
              else
              {
                  foreach (Exception loaderException in exception.LoaderExceptions)
                  {
                      System.IO.File.AppendAllText("C:\\TestLog.txt", loaderException.Message);
                      System.IO.File.AppendAllText("C:\\TestLog.txt", loaderException.StackTrace);
                  }
              }

}

Tags:

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading