Page 1 of 1

Reflection issue: Could not load file or assembly

Posted: Fri Aug 24, 2012 11:36 am
by John
Hallo Support,

I have an assembly/reflection issue for which I cannot find a solution, despite several hours of trying. We are using Stimulsoft.Report.dll (v2.0.50727) and Stimulsoft.Base.dll (v2.0.50727) in our c# code with .net Framework 4. Up to now, everything has been server-side (in .net) and has worked fine. We now wish to provide an offline-version of our product which means that everything will be installed on the client. The calling program is silverlight which must call the ReportGenerator .net class which then calls the Stimulsoft dlls to produce the pdf. Since silverlight cannot call .net directly we have decided to use a COM+ service, even if this sounds a bit old-fashioned.

So I now have a silverlight test program which calls the COM+ server dll, which in turn call my ReportGenerator, passing it the data it requires to produce a report. My ReportGenerator class creates the StiReport as usual and for a simple report, everything works fine: the pdf is created. The problem arises when I have a main report with sub-reports. Here I receive a "Could not load file or assembly" error (see below). On debugging, it seems that Stimulsoft.Base is calling methods in Stimulsoft.Report by reflection. When I call AppDomain.CurrentDomain.GetAssemblies() I can see that both dlls have been loaded. However, I also see that the event AppDomain.CurrentDomain.TypeResolve is being called many times. As I understand it, this event is only called when an attempt is made to call a method by reflection and the method or its assembly cannot be loaded. Here, I see that the base assembly is calling methods in the Stimulsoft.Report assembly.

I have tried re-loading the assembly from within the event and returning it. Unfortunately, this makes no difference and I end up with this error:

Loader exception
{"Could not load file or assembly 'Stimulsoft.Report, Version=2012.2.1302.0, Culture=neutral, PublicKeyToken=ebe6666cba19647a' or one of its dependencies. The system cannot find the file specified.":"Stimulsoft.Report, Version=2012.2.1302.0, Culture=neutral, PublicKeyToken=ebe6666cba19647a"}


at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at Stimulsoft.Report.StiReport.GetReportsFromAssembly(Assembly assembly)
at Stimulsoft.Report.StiReport.GetReportFromAssembly(Assembly assembly)
at Stimulsoft.Report.StiReport.CreateInstance()
at Stimulsoft.Report.StiReport.Compile(String path, Stream stream, StiOutputType outputType, Boolean autoCreate, Object standaloneReportType)
at Stimulsoft.Report.StiReport.Compile(String path, Stream stream, StiOutputType outputType, Boolean autoCreate)
at Stimulsoft.Report.StiReport.Compile(String path, StiOutputType outputType, Boolean autoCreate)
at Stimulsoft.Report.StiReport.Compile(String path, StiOutputType outputType)
at Stimulsoft.Report.StiReport.Compile(StiOutputType outputType)
at Stimulsoft.Report.StiReport.Compile()
at Stimulsoft.Report.Engine.StiReportV2Builder.RenderSubReports(StiReport ownerReport, StiRenderState renderState)
at Stimulsoft.Report.StiReport.RenderReport(StiRenderState renderState)
at Stimulsoft.Report.StiReport.Render(StiRenderState renderState, StiGuiMode guiMode)
at Stimulsoft.Report.StiReport.Render(StiRenderState renderState)
at Stimulsoft.Report.StiReport.Render(Boolean showProgress, Int32 fromPage, Int32 toPage)
at Stimulsoft.Report.StiReport.Render(Boolean showProgress)
at MM.Office.Services.ReportGenerator.ReportEngine.MergeReports()

Step into: Stepping over non-user code 'Stimulsoft.Base.StiCompiler.Compile'
Step into: Stepping over non-user code 'Stimulsoft.Report.StiReport.Compile'
A first chance exception of type 'System.Reflection.ReflectionTypeLoadException' occurred in Stimulsoft.Report.dll

----------------------
It looks as though StiTypeFinder.GetType is doing the reflection call using AppDomain.CurrentDomain.GetAssemblies().

Have you had this issue before and do you have any idea what I can do about it?

Thanks and regards,

John Kitching

Re: Reflection issue: Could not load file or assembly

Posted: Fri Aug 24, 2012 7:01 pm
by Ivan
Hello,

We have not encountered such a problem before.
Can you please send us a simple test project, which reproduce the issue?

Thank you.

Re: Reflection issue: Could not load file or assembly

Posted: Thu Sep 06, 2012 1:02 pm
by John
Hallo Ivan,

I've found the solution (despite many litres of coffee and days of trying).
In case anyone is interested or has the same problem, here's the solution:

http://stackoverflow.com/questions/2658 ... dexception

Now the explanation: Calling a COM+ object from Silverlight, which then calls a .net dll causes paths to become confused / lost when the code (in the .net assembly) tries to load assemblies using reflection. Everything was ok as long as the 2 Stimulsoft dlls I use (Base and Report) were both in the Global Assembly Cache. When I took them out, the .net environment no longer knew the correct path to find the dlls it required (this is how I understand it at least).
Now by adding
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
and the event implementation described in the link above, the assemblies are automatically correctly loaded when required.
Magic!

Regards,

John K.

Re: Reflection issue: Could not load file or assembly

Posted: Fri Sep 07, 2012 9:41 am
by HighAley
Hello.

It's great.
Thank you for solution.