Search code examples
c#asp.netreflectionasp.net-web-apitelerik-reporting

Instantiating a class produces ReflectionTypeLoadException at Assembly.GetTypes()


I discovered that after my application generates Telerik report

var result = new ReportProcessor().RenderReport("PDF", new InstanceReportSource { ReportDocument = new MyTelerikReport(data) }, null);
var stream = new MemoryStream(result.DocumentBytes);

return CreateHttpFileResponse("MyReport.pdf", stream, "application/pdf");

I am not able to get all types within CurrentDomain

var typesWithAttribute = (from a in AppDomain.CurrentDomain.GetAssemblies()
                          from t in a.GetTypes() //error appears here
                          //some filtering logic
                          select t).ToList();

I am getting error

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

LoaderExceptions:

System.IO.FileNotFoundException: Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. File name: 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

After some investigation I found that assembly that fails to load: Telerik.Reporting.OpenXmlRendering, Version=8.0.14.311, Culture=neutral, PublicKeyToken=a9d7983dfcc261be and that assembly doesn't exists in AppDomain.CurrentDomain.GetAssemblies() before I generate report (I assume that assembly loaded dynamically by Telerik.Reporting, Version=8.0.14.311, Culture=neutral, PublicKeyToken=a9d7983dfcc261be).

I could filter out that assembly as I don't need any types from that but I am a bit worried about fact of having assemblies in domain that cannot be loaded - seems a bit wrong to me.

Could someone explain what there happens? Is it my issue or that is fault of 3rd party library that doesn't load all required assemblies?


Solution

  • The issue is not the assembly but the Type coming from a dependent assembly that has not been loaded.

    If the GetTypes method is called on an assembly and a type in that assembly is dependent on a type in an assembly that has not been loaded (for example, if it derives from a type in the second assembly), a ReflectionTypeLoadException is thrown.

    http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes(v=vs.110).aspx