简体   繁体   中英

Clean Com components when host the asp.net website in IIS

I have a website hosted in IIS. My application is all about writing and reading data to/from Excel document.

When I run the application from VS,i'm seeing only one EXCEL.exe all the time But When I host the same application in IIS ,for every writing its creating one excel object and showing in Task-manager,Hence sometimes my website stops responding.

Below is my code

    try
            {
                xlApp = new app.Application();
                xlWorkBooks = xlApp.Workbooks;          
                xlWorkBook = xlWorkBooks.Open(FilePath, missing, false, missing, missing, missing, true, missing, missing, true, missing, missing, missing, missing, missing);   
                xlWorkSheet = xlWorkBook.Worksheets.get_Item(index);
//code …………………………….
                xlApp.DisplayAlerts = false;
                xlWorkBook.Save();
                boolWriteSuccess = true;
                xlApp.DisplayAlerts = true;
            }
Catch()
{
}            
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                Marshal.FinalReleaseComObject(xlWorkSheet);
                Marshal.FinalReleaseComObject(xlWorkSheets);
                xlWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
                Marshal.FinalReleaseComObject(xlWorkBook);
                xlWorkBooks.Close();
                Marshal.FinalReleaseComObject(xlWorkBooks);
                if (xlApp != null)
                    xlApp.Quit();
                Marshal.FinalReleaseComObject(xlApp);
            }

Here is the screen shot of Task Manager when website hosted in IIS 在此处输入图片说明

I want to clean the excel.exe from task manager always but some how I'm unable to clean. Please help me in this regard.

after saving the excel you should close the workbook. as each time you are creating nea app.application it will create new excel.exe after saving excel use below code

    try {
foreach (Process proc in Process.GetProcessesByName("Excel"))
        {
            proc.Kill();
        }}catch(Exception ex){
MessageBox.Show(ex.Message);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM