NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.
MIT License
700 stars 145 forks source link

NetOfficeFw not close Application if call by WebApi #392

Open heavymanto opened 1 year ago

heavymanto commented 1 year ago

NetOfficeFw 1.9.3 Framework .NET6.0 VS 2022 SO Win 11

I have buid a simple example for test convertion in Pdf file

public static void ConvertExcel2Pdf(string excelPath, string pdfPath)
        {
            Workbook wkb = null;
            NetOffice.ExcelApi.Application app = new NetOffice.ExcelApi.Application(true);

            try
            {
                app.DisplayAlerts = false;
                app.Visible = false;

                wkb = app.Workbooks.Open(excelPath);
                wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfPath);
            }
            finally
            {
                // Cleanup
                wkb.Close(false);
                wkb.Dispose();
                app.Quit();

                WaitForComCleanup();
            }
        }

It work fine and close process if i call this method by Console App, but if i call via WebAPI

[HttpGet()]
        [Route("api/GetConvert2Pdf")]
        public string GetConvert2Pdf(string fullPathInputFile = @"c:\temp\TemplateExcel.xlsx")
        {
            try
            {
                string testFileInput = fullPathInputFile;
                string testFileOutput = @"c:\temp\TemplateExcel.pdf";

                ConvetDocumentUtility.ConvertExcel2Pdf(testFileInput, testFileOutput);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "Finish OK!";
        }

seem not to able to close to process

image

there is a workarround?

Thanks

jozefizso commented 1 year ago

You must use memory profiler to test your application and track leaking references.

heavymanto commented 1 year ago

I'm sorry, have you an example? Thanks

M.F.