简体   繁体   English

RPC服务器不可用

[英]RPC server is unavailable

I'm getting an intermittent error when using Outlook interop within my program. 在程序中使用Outlook互操作时出现间歇性错误。 I get users reporting this error every now and then, but it's impossible to reproduce on my end. 我不时让用户报告此错误,但最终无法重现。 What's even stranger is that if they restart the program and try again, the error is gone. 更奇怪的是,如果他们重新启动程序并重试,则错误消失了。

Here's the code I'm using to get a reference to Outlook. 这是我用来获取Outlook引用的代码。

public class CommonStuff
{
    public static void Initialize()
    {
        olk = new Microsoft.Office.Interop.Outlook.Application();
        olk.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(OutlookInterop_ItemSend);
    }

    public static Microsoft.Office.Interop.Outlook.Application GetOutlook()
    {
        if (Process.GetProcessesByName("OUTLOOK").Count() == 0) //previous attempt at fixing this issue, and i'm not sure if i even need this
        {
            olk = new Microsoft.Office.Interop.Outlook.Application();
        }
        return olk;
    }
}

And the code that's used when sending the actual email. 以及发送实际电子邮件时使用的代码。

    public void SendEmail()
    {
        Microsoft.Office.Interop.Outlook.MailItem eMail = (Microsoft.Office.Interop.Outlook.MailItem)CommonStuff.GetOutlookApp().CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
        eMail.Subject = String.Format("Subject");
        eMail.To = "email@things.com";
        eMail.Companies = "Company";
        eMail.Body = "blah blah blah";
        eMail.Attachments.Add(GetCrystalReportPDF());
        ((Microsoft.Office.Interop.Outlook._MailItem)eMail).Display();
    }

Unfortunately I don't know where exactly the error is happening because I can't reproduce the bug. 不幸的是,我不知道错误在哪里发生,因为我无法重现该错误。 Does anyone have any clues as to what's going on? 有人对发生的事情有任何线索吗?

This is the classic kind of problem with process interop, you get to troubleshoot all of the bugs and crashes of that other process as well. 这是进程互操作的经典问题,您还需要解决该其他进程的所有错误和崩溃。 "RPC server is not available" is a very generic error and doesn't mean anything more than "the process doesn't work anymore". “ RPC服务器不可用”是一个非常常见的错误,除了“该进程不再起作用”以外,其含义不外。 A few simple reasons for that, it could simply have crashed or the user terminated it. 有几个简单的原因,它可能已经崩溃或用户终止了它。 Outlook is in general a troublemaker, it isn't exactly the most stable program in Office. 一般而言,Outlook是一个麻烦制造者,它并不是Office中最稳定的程序。

Your work-around with Process.GetProcessesByName() is a fair attempt but it is unlikely to work. 您使用Process.GetProcessesByName()的变通办法是一个合理的尝试,但不太可能。 Users commonly already have Outlook running for their own use, you'll see that instance as well. 用户通常已经可以运行Outlook以供自己使用,您还将看到该实例。 You can't tell if that process you see is the one that you started and matches your olk instance or is the one that user is looking at. 您无法确定您看到的进程是您启动并匹配olk实例的进程,还是用户正在查看的进程。

The proper workaround is to re-create your olk instance when you get the exception. 正确的解决方法是在收到异常时重新创建您的olk实例。 That can be hard to deal with since it might be generated while you are deeply nested in your code. 这可能很难处理,因为它可能是在您深深嵌套在代码中时生成的。 But it is best to not fix that case, because that hints at your code inducing the crash, you don't want to hide that problem. 但是最好不要修复这种情况,因为这暗示了代码导致崩溃,您不想隐藏该问题。 Implement a "canary test", just sniff at an innocent property before you start doing something non-trivial. 实施“金丝雀测试”,在开始做一些不平凡的事情之前,先对一个无辜的财产进行嗅探。 If that induces the exception then recreate the instance. 如果那会引发异常,请重新创建实例。 Do test this, I'm not 100% sure that the old olk instance is going to bomb your program when it gets finalized. 做这个测试,我不确定百分百确定旧的olk实例将在程序结束时轰炸您的程序。 You ought to get a repro by killing Outlook.exe with Task Manager. 您应该通过使用任务管理器杀死Outlook.exe来获得再现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM