Reconnecting to a remote server using DCOM
-
Hello all, I am writing a client for a remote windows service using C#. I first instantiate the remote object ... Type t = Type.GetTypeFromProgID(progId, serverName); APPLib.IApp obj = (APPLib.IApp)Activator.CreateInstance(type); Then I use the method and it works great .. obj.SomeMethod(); But if I unplug the network cable between the object creation and its use, I get a 0x800706BF COMException (The remote procedure call failed.) which is expected. The problem I have is after I reconnect the network cable and then try to recover by calling the Type.GetTypeFromProgID() and Activator.CreateInstance(type) methods, I repeatedly get "The remote procedure call failed." COM Exceptions in the Activator.CreateInstance() call. I can even start another instance of my client that connects to the remote server just fine while this instance is still throwing execptions. Any suggestions on how to recover gracefully? Thank you, Bob
-
Hello all, I am writing a client for a remote windows service using C#. I first instantiate the remote object ... Type t = Type.GetTypeFromProgID(progId, serverName); APPLib.IApp obj = (APPLib.IApp)Activator.CreateInstance(type); Then I use the method and it works great .. obj.SomeMethod(); But if I unplug the network cable between the object creation and its use, I get a 0x800706BF COMException (The remote procedure call failed.) which is expected. The problem I have is after I reconnect the network cable and then try to recover by calling the Type.GetTypeFromProgID() and Activator.CreateInstance(type) methods, I repeatedly get "The remote procedure call failed." COM Exceptions in the Activator.CreateInstance() call. I can even start another instance of my client that connects to the remote server just fine while this instance is still throwing execptions. Any suggestions on how to recover gracefully? Thank you, Bob
use try/catch:laugh:
-
use try/catch:laugh:
Thank you for the suggestion and I apologize for not being clearer. I do use try/catch blocks. I am attempting to implement some retry logic to handle the occasional network glitch. The CreateObject() method in the code below wraps the Type.GetTypeFromProgID() and Activator.CreateInstance() calls. The CreateObject() method is also the method I call to intially create the remote object (crudq in this code). What I do to create this problem is set a breakpoint on the crudq.DatabaseQuery call. When the breakpoint is reached (and the remote object crudq is successfully created), I unplug the network cable and hit F10 to execute the call. The crudq.DatabaseQuery() throws a RPC_S_CALL_FAILED COMException. I then plug the network cable back in then F10 through the code. Every call to CreateObject() throws a RPC_S_CALL_FAILED COMException when it calls Activator.CreateInstance(). Below is the code:
public void DatabaseQuery(string databaseName, int instance, Object input, Object filter, ref Object output, ref int result) { bool retry = true; int count = 0; int r = 0; Object o; do { try { crudq.DatabaseQuery(databaseName, instance, timeout, input, filter, out o, out r); retry = false; result = r; output = o; } catch(COMException c) { if (count < retryAttempts && (c.ErrorCode == RPC_S_CALL_FAILED || c.ErrorCode == RPC_S_SERVER_UNAVAILABLE)) { x.Info("DatabaseQuery: Retry count: " + count.ToString() + ", Exception: " + c.Message); Thread.Sleep(delayBetweenRetry); //Recreate COM object Object obj; try { CreateObject(progID, server, out obj); crudq = (AMSLib.ICRUDQ)obj; } catch { } ++count; } else { x.Error("DatabaseQuery: Exception caught: " + c.Message, c); throw; } } catch(Exception e) { x.Error("DatabaseQuery: Exception caught: " + e.Message, e); throw; } } while(retry); }
Thank you, Bob