COM Interop, RPC server is unavailable in c#

*爱你&永不变心* 提交于 2019-12-07 06:24:25

问题


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#.

So, can anybody tell me the possible reasons for the problem(RPC Server is Unavailable) and solutions to this problem.

I am helpless with this issue by now.

So, Advance thanks if you can help me out


回答1:


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();
}

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.



来源:https://stackoverflow.com/questions/16588201/com-interop-rpc-server-is-unavailable-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!