COM Interop, RPC server unavailable in C #

I use COM interoperability and I create an instance of a COM class object from dll interop. Thus, several times the object is created successfully and makes remote procedure calls without any problems, but sometimes it throws an exception such as RPC Server is unavailable. The COM component that I use is written in VB, and I consume this component in C #.

So, can someone tell me the possible causes of the problem (RPC server is unavailable) and solutions to this problem.

I am now helpless with this problem.

So thanks for the help if you can help me

+5
source share
1 answer

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.

+6
source

All Articles