相关文章推荐
读研的人字拖  ·  Java 读取 .properties ...·  3 天前    · 
犯傻的绿茶  ·  clickhouse ...·  昨天    · 
老实的玉米  ·  springboot+kafka中@Kafk ...·  昨天    · 
讲道义的大海  ·  ASP.NET Core の Razor ...·  22 小时前    · 
聪明的海龟  ·  前端react ...·  2 月前    · 
从未表白的啄木鸟  ·  Reason: Failed to ...·  1 年前    · 
睿智的核桃  ·  托管和部署ASP.NET Core ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

Ask Question
System.Runtime.InteropServices.COMException (0x800706BA): 
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

is received in my Windows Service when I run it on any machine, but when I test it in a test application no exception is thrown. In my code what I am doing is converting two .DAT files into .xls files. When I start the service through services.msc and run the service it runs fine for some time, but after updating certain rows it throws the exception and then nothing happens after that. I have got two separate functions that does the work separately. The sample code is:

     public void SaveData_component(string filename)
            string filepath = System.Configuration.ConfigurationSettings.AppSettings["filepath"].ToString() + filename;
            filepath_first = filepath;
            object missing = Missing.Value;
            //string getExtension = Path.GetExtension(filepath);
            string getFilename = Path.GetFileNameWithoutExtension(filepath) + "New";
            string filepathNew = System.Configuration.ConfigurationSettings.AppSettings["filepath"].ToString() + getFilename + ".xls";
                xlAppNew1 = new Application();
                xlAppNew1.DisplayAlerts = true;
                workbooks1 = xlAppNew1.Workbooks;
                workbook1 = workbooks1.Open(@filepath, 0, true, 1, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                // xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook1.Worksheets.get_Item(1);
                xlAppNew1.ActiveWorkbook.SaveAs(@filepathNew, -4143, "", "", false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                missing, missing, missing, missing, missing);
                string getExtension = ".xls";//Path.GetExtension(filepathnew);
                //string getFilename = Path.GetFileNameWithoutExtension(filepathnew);
                string connString = "";
                if (getExtension.ToLower() == ".xls")
                    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepathNew + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
                    connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepathNew + ";Extended Properties=Excel 12.0 Xml;HDR=Yes;IMEX=1;";
                OleDbConnection con = new OleDbConnection(connString);
                con.Open();
                System.Data.DataTable dtSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string tname = dtSheet.Rows[0]["TABLE_NAME"].ToString();
                OleDbDataAdapter ad = new OleDbDataAdapter(@"Select * FROM [" + tname + "];", con);
                DataSet dset = new DataSet();
                ad.Fill(dset, "ProductOrderBOM");
                System.Data.DataTable dt = new System.Data.DataTable();
                System.Data.DataTable dttocopy = new System.Data.DataTable();
                dt = dset.Tables["ProductOrderBOM"];
                if (dt != null || dt.Rows.Count > 0)
                    dttocopy.Columns.Add("Column1", typeof(string));
                    dttocopy.Columns.Add("Column2", typeof(string));
                    dttocopy.Columns.Add("Column3", typeof(string));
                    dttocopy.Columns.Add("Column4", typeof(string));
                    dttocopy.Columns.Add("Column5", typeof(string));
                    dttocopy.Columns.Add("Column6", typeof(string));
                    dttocopy.Columns.Add("Column7", typeof(string));
                    dttocopy.Columns.Add("Column8", typeof(string));
                    dttocopy.Columns.Add("Column9", typeof(string));
                    for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
                        dttocopy.Rows.Add(dt.Rows[iRow][0].ToString().Substring(3, 9), dt.Rows[iRow][0].ToString().Substring(12, 4), dt.Rows[iRow][0].ToString().Substring(16, 18), dt.Rows[iRow][0].ToString().Substring(34, 8), dt.Rows[iRow][0].ToString().Substring(42, 4), dt.Rows[iRow][0].ToString().Substring(46, 18), dt.Rows[iRow][0].ToString().Substring(64, 40), dt.Rows[iRow][0].ToString().Substring(104, 3), dt.Rows[iRow][0].ToString().Substring(107, 5));
                    foreach (DataRow item in dttocopy.Rows)
                        if (item.ItemArray[0].ToString() != "" && item.ItemArray[5].ToString() != "" && item.ItemArray[8].ToString() != "")
                            string prdorderno = item.ItemArray[0].ToString().Trim();
                            string materialcode = item.ItemArray[5].ToString().Trim();
                            double qty = Convert.ToDouble(item.ItemArray[8].ToString().Trim());
                            d1 = callprocedure(prdorderno, materialcode, Math.Round(qty, 2));
                            if (d1 != null)
                                if (d1.Tables[0].Rows[0]["Column1"] != null)
                                     WriteStuff(d1.Tables[0].Rows[0]["Column1"].ToString());
                d1.Clear();
                d1.Dispose();
                dset.Clear();
                dset.Dispose();
                dtSheet.Clear();
                dtSheet.Dispose();
                dt.Clear();
                dt.Dispose();
                dttocopy.Clear();
                dttocopy.Dispose();
                ad.Dispose();
                con.Close();
                con.Dispose();
            catch (Exception Ex)
                    WriteStuff(Convert.ToString(Ex) + "save_datacomponent function before finally");
            finally
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (workbooks1 != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks1);
                if (workbook1 != null)
                    workbook1.Close(Type.Missing, Type.Missing, Type.Missing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1);
                if (xlAppNew1 != null)
                    xlAppNew1.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlAppNew1);

This is the function where the work is done.Any help would be greatly appreciable. My Writestuff() Method is catching the following Exception:

    System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) at Microsoft.Office.Interop.Excel.WorkbookClass.Close(Object SaveChanges, Object Filename, Object RouteWorkbook)at MyNewService.MyNewService.SaveData_component(String filename)savedata_component functionSystem.IO.FileNotFoundException: Could not find file 'C:\SUMIT\COMPONENT_TAI_PT1_RMKH_3799_20130603_030504New_03-06-2013-18-07-09-537_04-06-2013-16-42-20-194.DAT'.

Also if I get rid of the clear methods in my code and the

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject();

Another exception comes in my way:

    System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.at Microsoft.Office.Interop.Excel.WorkbookClass.Close(Object SaveChanges, Object Filename, Object RouteWorkbook)at MyNewService.MyNewService.SaveData_component(String filename)

I am really not sure what the hell is hapenning.

The line that begins dttocopy.Rows.Add(... is suspect because it presumes that the length of the string it finds is always >= 4 (it uses 3 index in SubString()). What if that is not true? You get an exception. You should first test dt.Rows[iRow][0] string length before proceeding with SubString() operations. – DonBoitnott Jun 4, 2013 at 11:19 Sigh, these questions never end. Easy enough to diagnose the problem, all you need is Task Manager. You'll see the dozens of instances of Excel.exe that your code doesn't actually managed to release. Get rid of your global "xl" variables and make them local variables. Get rid of all these ineffective Clear() and ReleaseComObject() calls. Test your code to ensure that Excel.exe stops running after the GC.Collect + WaitForPendingFinalizers calls. Do not test your Debug build, only the Release build without a debugger attached. – Hans Passant Jun 4, 2013 at 11:34 Ok I got it, my Task Manager did had dozens of unclosed EXEL.EXE opened. I will give you the Error that my Writestuff() method is catching. I am not having any clue as to where I am going wrong.I have closed every instance of COM objects that I have created. Can you please tell me if I have done something wrong in releasing the COM objects. – Sumit Roy Jun 4, 2013 at 12:16 How can I get rid of the Excel.exe that are being generated. I did as told by you but still one Excel.exe by system remains and the same error persists. – Sumit Roy Jun 4, 2013 at 12:31

http://support.microsoft.com/kb/257757

While the article states that Office 2003 support has ended, the contents are still valid today: Microsoft does not support server-side automation of Office. Even in client-side automation Office interop is pretty quirky.

You're best off with a library such as Aspose or NPOI.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.