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