在Excel中启用COM加载项会导致“RPC服务器不可用”异常 [英] COM Add-in enabled in Excel causes 'RPC server is unavailable' exception

查看:493
本文介绍了在Excel中启用COM加载项会导致“RPC服务器不可用”异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#应用​​程序使用Interop dll从/中读取/写入内容。


最近我开始看到'RPC服务器不可用'例外。以下是一些调查结果  -

My C# application reads/writes content from/into an excel using Interop dll.
Recently I started seeing 'RPC server is unavailable' exception. Following are some of the findings -

1。弄清楚这是因为添加到excel的新COM加载项。从excel禁用此特定加载项后,例外情况就消失了。 (Excel启用了其他几个COM加载项)

1. Figured out that it's because of a new COM add-in that got added to excel. The exception goes away on disabling this particular add-in from excel. (Excel has several other COM add-ins enabled)

2.启用新加载项启用 - 当我调试所有excel 代码的部分时;对象创建完成(按F10),没有看到问题

2. Leaving the new add-in enabled -  when I debug the part of the code where all the excel objects creation is done (pressing F10), the issue is not seen

3。添加了几个控制台语句,并找出问题发生的位置 -


workBooks.Open(excelFileName,0,false,Type.Missing,"", "",false,Type.Missing,",quot,true,false,0,true,false,false);

3. Added few console statements and figured out the line where the issue happens -
workBooks.Open(excelFileName, 0, false, Type.Missing, "", "", false, Type.Missing, "", true, false, 0, true, false, false);

当我添加一个睡眠时间在这个陈述之前约500毫秒,这个问题就消失了。

When I add a sleep time of ~500ms before this statement, the issue goes away.

有人可以指导我为什么会有一些延迟吗?是否建议将此延迟添加到代码中?我应该尝试查看加载项代码并在那里修复一些内容吗?

Can someone guide me as to why it works with some delay? Is it advisable to add this delay to the code? Should I try to look at the add-in code and fix something there?

推荐答案

Hello Pavithra,

Hello Pavithra,

首先,我建议发布所有底层COM对象立即生效。您可以使用

Marshal.ReleaseComObject
方法。它只是递减指定
运行时的引用计数Callable Wrapper (RCW)与指定的COM对象关联。例如:

First of all, I'd recommend releasing all underlying COM objects instantly. You can use the Marshal.ReleaseComObject method for releasing references. It just decrements the reference count of the specified Runtime Callable Wrapper (RCW) associated with the specified COM object. For example:

// instead of writing the following code
application.Workbooks.Open(filename)
// break it into separate lines
workbooks = application.Workbooks;
workbook = workbooks.Open(filename);
// then release objects
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(workbook);

另请参阅与Word相关的类似论坛帖子  Microsoft.office .interop
word和RPC服务器不可用。 (来自HRESULT的异常:0x800706BA)

。但它描述了同样的问题。我认为这个问题对于所有Office应用程序都很常见,而且与COM技术有关。

Also please take a look at the similar forum thread related to Word Microsoft.office.interop word and The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) . But it describes the same issue. I suppose the issue is common for all Office applications and it's related to the COM technology.

您可能对
Excel 2010 - C# - 有时我也会得到一个错误0x800706BA
线程。


这篇关于在Excel中启用COM加载项会导致“RPC服务器不可用”异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆