当在特定机器上运行时,我收到上述错误,它会暂停我的应用程序中的活动。当我在自己的机器上运行它时,不会发生这样的错误。
也许“ RPC 服务器不可用”是问题的症结所在,但是在应用程序之前运行(并且仍在我的机器上运行)之后会导致它弹出?
在更多上下文中(显示似乎有价值/重要的内容),错误消息是:
System.InvalidCastException:无法将“Microsoft.Office.Interop.Outlook.ApplicationClass”类型的 COM 对象转换为接口类型“Microsoft.Office.Interop.Outlook._Application”。此操作失败,因为 IID 为“{00063001-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用由于以下错误而失败:RPC 服务器不可用。(来自 HRESULT 的异常:0x800706BA)。在 System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease) 在 Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType) 在 RoboReporter2017.ExceptionLoggingService.EmailMessageToAssignee(String unit, String notificationRecipient,字符串 rptName) 在 RoboReporter2017。RoboRprtrLib.GenerateAndSaveDueReports() 在 RoboReporter2017.FormMain.RunDueReports() 在 RoboReporter2017.FormMain.FormMain_Load(Object sender, EventArgs e) 。. .
************** 加载的程序集 ************** -------------------- -------------------- Microsoft.Office.Interop.Outlook 程序集版本:12.0.0.0 Win32 版本:12.0.4518.1014 代码库:file:///C:/Windows /assembly/GAC/Microsoft.Office.Interop.Outlook/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll ------------------------ ----------------- office 程序集版本:12.0.0.0 Win32 版本:12.0.4518.1014 代码库:file:///C:/Windows/assembly/GAC/office/12.0。 0.0__71e9bce111e9429c/office.dll ----------------------------------------
如 err msg 中所引用的,在该机器上中断的方法是:
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,与错误消息中列出的“已加载程序集”中列出的版本相同。
更新
考虑到 Outlook 未运行可能是问题所在,我编写了以下代码:
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();
}
...从这里改编,但在实施之前,我关闭了 Outlook 并运行了应用程序,看看如果 Outlook 没有运行,我是否会在我的机器上收到相同的错误消息。但不是!它会自行重新启动 Outlook,而无需我花哨的 StartOutlookIfNotRunning() 方法。
所以这不是问题,反正...