After reviewing my approach to implementing COM, I found an error. I used a static class to initialize a COM instance, and the initialization material took place in a static constructor. Thus, initialization was performed once per application session. In the case when the com instance is damaged or deleted, then a call to the COM methods raises an exception (the RPC server is unavailable). So, I used the following approach to overcome the problem.
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 the COM instance cannot make the call, delete the instance and reinitialize the COM and get the instance, and then call the RPC server.
Yogesh joshi
source share