簡體   English   中英

System.Runtime.InteropServices.COMException(0x800706BA):RPC服務器不可用。 (HRESULT異常:0x800706BA)

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

這個例外

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

當我在任何機器上運行它時,在我的Windows服務中收到,但是當我在測試應用程序中測試它時,不會拋出任何異常。 在我的代碼中,我正在做的是將兩個.DAT文件轉換為.xls文件。 當我通過services.msc啟動服務並運行服務時,它運行正常一段時間,但在更新某些行后,它會拋出異常,然后沒有任何反應。 我有兩個獨立的功能,分別完成工作。 示例代碼是:

     public void SaveData_component(string filename)
    {
        try
        {
            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";

            try
            {
                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;\"";

                else
                    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);
                }


            }

這是工作完成的功能。任何幫助都會非常明顯。 我的Writestuff()方法正在捕獲以下異常:

    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'.

如果我擺脫了我的代碼中的清晰方法和

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

另一個例外是我的方式:

    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)

我真的不確定到底是怎么回事。

嘗試按照本文中介紹的第二種配置方法

http://msdn.microsoft.com/en-us/library/aa389286.aspx

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

雖然文章指出Office 2003支持已經結束,但今天的內容仍然有效:Microsoft不支持Office的服務器端自動化。 即使在客戶端自動化Office互操作也非常古怪。

您最好使用Aspose或NPOI等庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM