我该如何修复 "Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable"?

How can I fix "Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable"?

我收到上述错误,当在特定机器上 运行 时,我的应用程序会暂停 activity。当我 运行 它在我自己的机器上时,没有出现这样的错误。

也许“RPC 服务器不可用”是问题的症结所在,但是在应用程序之前运行(并且仍在我的机器上运行)之后会弹出什么原因)?

错误消息,在更多上下文中(显示似乎是 value/import 的内容)是:

System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA). at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease) at Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType) at RoboReporter2017.ExceptionLoggingService.EmailMessageToAssignee(String unit, String notificationRecipient, String rptName) at RoboReporter2017.RoboRprtrLib.GenerateAndSaveDueReports() at RoboReporter2017.FormMain.RunDueReports() at RoboReporter2017.FormMain.FormMain_Load(Object sender, EventArgs e) . . .

************** Loaded Assemblies ************** ---------------------------------------- Microsoft.Office.Interop.Outlook Assembly Version: 12.0.0.0 Win32 Version: 12.0.4518.1014 CodeBase: file:///C:/Windows/assembly/GAC/Microsoft.Office.Interop.Outlook/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll ---------------------------------------- office Assembly Version: 12.0.0.0 Win32 Version: 12.0.4518.1014 CodeBase: file:///C:/Windows/assembly/GAC/office/12.0.0.0__71e9bce111e9429c/office.dll ----------------------------------------

在错误消息中引用的那台机器上发生故障的方法是:

internal static void EmailMessageToAssignee(string unit, string notificationRecipient, string rptName)
{
    string saveLocation = @"\storageblade\cs\REPORTING\RoboReporter";
    var subject = string.Format("Your {0} report for {1} generated by Robo Reporter 2017", rptName, unit);
    var body = string.Format("Your {0} report for {1} was generated by Robo Reporter 2017 and can be found in the usual location in the shared network folder ({2})", rptName, unit, saveLocation);

    Application app = new Application();
    MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
    mailItem.To = notificationRecipient;
    mailItem.Subject = subject;

    mailItem.HTMLBody = string.Format(@"<html><body><img src='http://www.proactusa.com/bla/images/pa_logo_notag.png' alt='Platypus logo' width='199' height='130' ><p>{0}</p></body></html>", body);

    mailItem.Importance = OlImportance.olImportanceNormal;
    mailItem.Display(false);
    mailItem.Send();
}

我注意到我的项目参考中的 Microsoft.Office.Interop.Outlook 版本是 12.0.0.0,与错误消息中列出的 "Loaded Assemblies" 中列出的版本相同。

更新

可能是 Outlook 不是 运行ning 的问题,我写了这段代码:

private static void StartOutlookIfNotRunning()
{
    string OutlookFilepath = @"C:\Program Files (x86)\Microsoft 
Office\Office12\OUTLOOK.EXE";
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0) return;
    Process process = new Process();
    process.StartInfo = new ProcessStartInfo(OutlookFilepath);
    process.Start();
}

...改编自 here,但在实施之前,我关闭了 Outlook 和 运行 应用程序,以查看如果 Outlook不是 运行宁。但不是!它会自行重新启动 Outlook,不需要我花哨的 StartOutlookIfNotRunning() 方法。

所以这不是问题,无论如何...

参见FIX: Error message when you run an application that makes "burst load" style activation requests and calls a server component through DCOM: "0x800706ba" or "0x800706bf"

您何时何地尝试自动化 Outlook?

Microsoft 目前不推荐也不支持来自任何无人值守的 non-interactive 客户端应用程序或组件(包括 ASP、ASP.NET、DCOM、和 NT 服务),因为当 Office 在此环境中 运行 时,Office 可能表现出不稳定的行为 and/or 死锁。

如果您要在 server-side 上下文中构建 运行 的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到至少允许部分代码为 运行 client-side 的替代方案。如果您使用来自 server-side 解决方案的 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。在 Considerations for server-side Automation of Office 文章中阅读更多相关信息。

嗯,虽然 Eugene Astafiev 的建议很中肯,但在两次我都撞上了不祥的

System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'.

我根据 another forum 中 Eugene Astafiev 的建议解决了这个问题:

regtlib msoutl.olb

从 Office App 文件夹内提升的命令提示符。

对我来说,问题是在某些机器上可以正常工作,而在另一台机器上不能正常工作。