简体   繁体   English

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

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

This exception 这个例外

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. 当我在任何机器上运行它时,在我的Windows服务中收到,但是当我在测试应用程序中测试它时,不会抛出任何异常。 In my code what I am doing is converting two .DAT files into .xls files. 在我的代码中,我正在做的是将两个.DAT文件转换为.xls文件。 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. 当我通过services.msc启动服务并运行服务时,它运行正常一段时间,但在更新某些行后,它会抛出异常,然后没有任何反应。 I have got two separate functions that does the work separately. 我有两个独立的功能,分别完成工作。 The sample code is: 示例代码是:

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


            }

This is the function where the work is done.Any help would be greatly appreciable. 这是工作完成的功能。任何帮助都会非常明显。 My Writestuff() Method is catching the following Exception: 我的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'.

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. 我真的不确定到底是怎么回事。

Try to following the second configuration method explained in this article 尝试按照本文中介绍的第二种配置方法

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

http://support.microsoft.com/kb/257757 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. 虽然文章指出Office 2003支持已经结束,但今天的内容仍然有效:Microsoft不支持Office的服务器端自动化。 Even in client-side automation Office interop is pretty quirky. 即使在客户端自动化Office互操作也非常古怪。

You're best off with a library such as Aspose or NPOI. 您最好使用Aspose或NPOI等库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理 System.Runtime.InteropServices.COMException (0x800706BA):RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA) - How to handle System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) WMI RPC服务器不可用。 (来自HRESULT的异常:0x800706BA) - WMI The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) RPC服务器不可用。 (来自HRESULT的异常:0x800706BA)-Excel - The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) - Excel RPC服务器不可用。 连接到远程计算机时(来自HRESULT的异常:0x800706BA) - The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) when connecting to remote computer WMI:RPC服务器不可用。 (尝试连接到远程计算机时抛出HRESULT异常:0x800706BA) - WMI: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) throws when try to connect to remote machine LDAP 从域外重置密码。网络 C# 错误:RPC 服务器不可用。 (hresult 的异常:0x800706ba) - LDAP reset password from outside the domain network C# Error: RPC server is unavailable. (exception from hresult: 0x800706ba) 如何修复“CCertRequest::Submit:RPC 服务器不可用。0x800706ba”错误? - How to fix "CCertRequest::Submit: The RPC server is unavailable. 0x800706ba" error? 在错误0x800706BA之前捕获RPC锁定 - Catch RPC lock before error 0x800706BA 获取contact.LastName时System.Runtime.InteropServices.COMException(0x800706BE) - System.Runtime.InteropServices.COMException (0x800706BE) when getting contact.LastName InternetExplorer COMException: System.Runtime.InteropServices.COMexception: RPC 服务器不可用 - InternetExplorer COMException: System.Runtime.InteropServices.COMexception: The RPC server is unavailable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM