简体   繁体   English

COM Interop,RPC服务器在c#中不可用

[英]COM Interop, RPC server is unavailable in 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. 我正在使用COM Interop,我正在从interop dll实例化COM类对象所以,几次对象被成功实例化并进行远程过程调用没有任何问题,但有时它抛出一个异常,如RPC Server是不可用的。 The COM Component i am using is written in VB and i am consuming that component in c#. 我正在使用的COM组件是用VB编写的,我在c#中使用该组件。

So, can anybody tell me the possible reasons for the problem(RPC Server is Unavailable) and solutions to this problem. 那么,任何人都可以告诉我问题的可能原因(RPC服务器不可用)和解决此问题的方法。

I am helpless with this issue by now. 我现在对这个问题很无奈。

So, Advance thanks if you can help me out 所以,如果你能帮助我,请致谢

After reviewing my approach for COM implementation I found the bug. 在审查了我的COM实现方法后,我发现了这个bug。 I was using a static class for initializing the COM instance and initialization stuff was taking place in static constructor. 我使用静态类初始化COM实例,初始化的东西发生在静态构造函数中。 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). 在这种情况下,当com实例被破坏或被丢弃时,调用COM方法会引发异常(RPC Server不可用)。 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();
}

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的实例无法进行调用,则配置实例并重新初始化COM并获取实例,然后调用RPC Server。

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

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