0

I have an asp.net MVC Webapplication who uses the excel interop to open a csv in an excel save the file as an xlsx and load the bytes from that file. These bytes are then passed as a download.

So far everything works as expected. but after the 6 download I get the following exception. Unfortunately it's on german, since the server is setup in german. But the exception message is something like "80080005 starting the server failed".

Die COM-Klassenfactory für die Komponente mit CLSID {00024500-0000-0000-C000-000000000046} konnte aufgrund des folgenden Fehlers nicht abgerufen werden: 80080005 Starten des Servers fehlgeschlagen (Ausnahme von HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

    Beschreibung: Unbehandelte Ausnahme beim Ausführen der aktuellen Webanforderung. Überprüfen Sie die Stapelüberwachung, um weitere Informationen über diesen Fehler anzuzeigen und festzustellen, wo der Fehler im Code verursacht wurde. 

    Ausnahmedetails: System.Runtime.InteropServices.COMException: Die COM-Klassenfactory für die Komponente mit CLSID {00024500-0000-0000-C000-000000000046} konnte aufgrund des folgenden Fehlers nicht abgerufen werden: 80080005 Starten des Servers fehlgeschlagen (Ausnahme von HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

When I restart the website, it works again for the next 5-7 downloads.

here is my code for the excel:

private byte[] WriteExcel(SelectionResult result)
{
        var csvPath = Path.Combine(Settings.Default.FileGenerationPath, DateTime.Now.ToString("yyMMMddhhmmss") + "_" +new Random().Next(1000, 9999)+".csv");
        var excelPath = Path.Combine(Settings.Default.FileGenerationPath, DateTime.Now.ToString("yyMMMddhhmmss") + "_" + new Random().Next(1000, 9999)+".xlsx");

        var excelApp = new Application();
        File.WriteAllBytes(csvPath, WriteCsv(result, true));

        var excelWorkbooks = excelApp.Workbooks;
        excelWorkbooks.Open(csvPath, Delimiter: ",");
        var workbook = excelApp.ActiveWorkbook;

        workbook.SaveAs(excelPath, XlFileFormat.xlOpenXMLWorkbook);

        excelWorkbooks.Close();
        excelApp.Quit();

        var bytes = File.ReadAllBytes(excelPath);
        File.Delete(csvPath);
        File.Delete(excelPath);
        return bytes;
    }

What do I miss?

NPadrutt
  • 3,619
  • 5
  • 24
  • 60

1 Answers1

1

According to Microsoft Support, that kind of error

You may receive an "Error code 80080005 -- server execution failed." error message when you start many COM+ applications

arise when you are using too many com applications. And you have a pretty much not disposed unmanaged resources. Every time use using statement in you are using input-output operations.

Update No. You don't. Closing an Excel is not releasing COM object while the application is running.

You need to:

        //include marshal
        using System.Runtime.InteropServices;
        //release objects
        Marshal.ReleaseComObject(excelApp);
        Marshal.ReleaseComObject(excelWorkbooks);
        Marshal.ReleaseComObject(workbook);
SouXin
  • 1,565
  • 11
  • 17
  • But do I not dispose the com objects when I close the excelApp? – NPadrutt Jun 07 '17 at 17:51
  • @NPadrutt No, you need to release the COM objects (see here: https://stackoverflow.com/questions/158706/how-do-i-properly-clean-up-excel-interop-objects). Personally, I can't stand Excel Interop, and I'd recommend an Excel library (e.g. ClosedXML, EPPlus, NPOI, etc) instead. – C. Helling Jun 07 '17 at 17:59
  • Thanks your answer and comments. I will also have a look at the libraries :) – NPadrutt Jun 07 '17 at 18:23