5

We're having a problem in our ASP.Net application where the Crystal Reports engine leaves garbage .tmp files in the server's Temp folder after generating reports for the users.

So we're trying to figure out how to run the .Close() and .Dispose() methods on the Report object, but we're finding that the code never gets run after the export occurs.

MyReport.Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, 
        this.Response, true, "My_Report");

MyReport.Report.Close();
MyReport.Report.Dispose();

Breakpoints set on the last two lines never get hit, and we've also tried putting other code there to test the processing. None of it runs. (I've also seen this question on other sites with similar code, but no answers)

I assume the ExportToHttpResponse method is returning a file stream (PDF) to the user at that point, ending the processing so the rest of the code doesn't get run. If that's the case, how do we get CR to perform a cleanup on the temp files, which the Close() and Dispose() methods are supposed to do? Do we have to implement a manual after-the-fact cleanup?

Raelshark
  • 2,975
  • 3
  • 27
  • 36
  • I hope you get an answer. One of our vendors apparently was not aware of this issue, so our webserver stopped working one day when the disk filled up. We just run a scheduled task now to delete old temp files. – Peter Recore Feb 11 '10 at 21:01
  • Yep, exactly. Our hosted server has crashed a couple of times because of full disk space. Now the hosting team manually cleans up the files a couple of times a year. A scheduled task is a fallback we've considered. – Raelshark Feb 11 '10 at 21:50

2 Answers2

4

I don't have a way of reproducing this issue so I'll throw out there that you may be able to use the using statement which allows you to specify when objects should be released.

What is the C# Using block and why should I use it? http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx

Haven't tried this, but I think you might be able to do something like

using(MyReport m = new MyReport())
{
   m.Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, 
        this.Response, true, "My_Report");
}

As I'm typing it I'm not sure that it would be much different than what you have already, but oh well, it's something to try. In my head this works. :)

Hope it helps.

Community
  • 1
  • 1
Dusty
  • 4,667
  • 3
  • 29
  • 35
  • Thanks. We had thought of this as a possibility, and it seems like it would work. The other approach we're trying is to use Page_Unload. – Raelshark Feb 18 '10 at 19:11
4

I actually had this exact problem and found that you can force the Close() and Dispose() methods to run by enclosing the exporting method in a Try Catch block and placing the Close() and Dispose() methods in the Finally like this:

            Try
            MyReport.Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, True, ReportName)
        Catch ex As System.Exception

        Finally

            MyReport.Report.Close()
            MyReport.Report.Dispose()

        End Try
Jesif
  • 41
  • 1