COM Interop, RPC server is unavailable in c#

孤街醉人 提交于 2019-12-05 10:31:02

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.

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