Thursday, July 7, 2011

Report Rendering Fails in Local Mode

A desktop program developed using .NET 3.5 framework and was using RDLC report in local mode and a ReportViewer control  was throwing what might seem a weird exception the exception looked something like the below

The definition of the report 'Main Report' is invalid. 
Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: 
An unexpected error occurred in Report Processing. 
System.UnauthorizedAccessException: Access to the path 'C:\Documents and Settings\\Local Settings\Temp\expression_host_05cd5e1af4624646bc2dd846b60460f0.dll' is denied.

What is this expression_host_....dll file and Why is The Report Rendering Engine Trying to Access It?

When you write an expression in your report definition (any expression even something that looks like =Fields!FirstName.Value + " " + Fields!LastName.Value) or when you use Custom Code, the report rendering engine does two things; it seems to me. One the rendering engine extracts all custom expressions and custom code and, based on these custom expressions and code, it dynamically creates VB.NET classes which are then compiled on the fly to a .NET assembly. These files and the resulting assembly are placed in the %TEMP% directory; they are deleted once they are no longer needed by the current instance of the running report. The second thing the rendering engine does is referencing the dynamically created assembly (which is named expression_host_.dll) when rending your report and making the necessary calls to this assembly as dictated by your report definition. 

How is The Above Explanation Related to The Error In Question?


Some anti-malware/anti-virus software do not like programs creating dynamic DLLs, it seems. In my case I verified that the dynamically created assembly was indeed blocked by the anti-malware program so that the ReportViewer and the hosting application could not use it and the even rendering engine program could not delete the expression_host dll file; which it normally does once the report is done rendering.

As soon as stopped the anti-malware program or just told to trust certain pattern of files, the report rendered perfectly without a problem.

Finally, it's worth mentioning that this problem was sporadic and had no predictable pattern. That is, one out 20 trials  the report would render fine but most of the time it fails. Something which leads to believe that there was a "race" between the report rendering engine and its application and the anti-malware program. Sometimes the anti-malware program would miss the host_expression dll and the report would render fine but most of the time the dll files is blocked.

The Bottom Line:


If you're running a program that use a RLDC report in local mode and the report uses expressions and custom code, rendering the report could fail due to an anti-virus/ anti-malware program blocking the custom assembly created to host your expressions and/or custom code.