COM Interop,RPC服务器在c# [英] COM Interop, RPC server is unavailable in c#

查看:271
本文介绍了COM Interop,RPC服务器在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个COM Interop和我实例化的COM类对象从interop dll
所以,几次对象成功实例化,并进行远程过程调用没有任何问题,但有时它会抛出一个异常RPC服务器不可用。
我使用的COM组件是用VB编写的,我在c#中使用该组件。

I am using a COM Interop and i am instantiating the COM class object from the interop dll So, few times the object is instantiated successfully and make remote procedure calls without any problem but sometimes it throws an exception like RPC Server is unavilable. The COM Component i am using is written in VB and i am consuming that component in c#.

所以,任何人都可以告诉我可能的原因(RPC服务器不可用)和此问题的解决方案。

现在无法处理此问题。

如果你能帮助我,请提前感谢。

So, Advance thanks if you can help me out

推荐答案

我发现了错误。我使用一个静态类初始化COM实例和初始化东西正在静态构造函数。因此,每个应用程序会话都进行一次初始化。在这种情况下,当com实例被破坏或处置,然后调用COM方法throws exception(RPC服务器不可用)。
所以,我使用以下方法来克服这个问题

After reviewing my approach for COM implementation I found the bug. I was using a static class for initializing the COM instance and initialization stuff was taking place in static constructor. So, initialization was being done once per application session. In the case, when the com instance gets corrupted or is disposed, then making calling to COM methods throws exception (RPC Server is unavailable). So, I used following approach for overcoming the issue

 try
  {
    m_COMObject.SomeMethod();
  }

  Exception(exception exception)
  {
    DisposeCOMObject();
    InitializeCOMOBject();
    COMObject.Somethod();
  }


 public void DisposeCOMObject()
{
  m_COMObject = null;
  var process = Process.GetProcessesByNames("COM .exe").FirstDefault();

   if(process != null)
    {
         process.kill();
       }
}


 public void InitializeCOMObject()
{
  m_COMObject = null;
  m_COMObject = new COMObject();
}

如果COM实例无法调用,请处理实例并重新初始化COM并获取实例,然后调用RPC服务器。

if instance of COM is unable to make call then dispose the instance and reinitialize the COM and get instance, then make call to RPC Server.

这篇关于COM Interop,RPC服务器在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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