excel interop unable to start server exception after several calls

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?

c#
asp.net
excel
asked on Stack Overflow Jun 7, 2017 by NPadrutt

1 Answer

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);
answered on Stack Overflow Jun 7, 2017 by SouXin • edited Jun 7, 2017 by SouXin

User contributions licensed under CC BY-SA 3.0