views:

1205

answers:

2

I have an object created in a host application and can accecss it remotely using remoting, is there any way I can test the connection to ensure it is still "alive"? Maybe an event I can use that fires if the remoting connection gets disconnected, or some property that can tell me the state of the remoting connection. Is there something like this available?

+5  A: 

I generally add another method to the remoting server MarshallByRef class, (I generally name it Ping(), as in:

 public void Ping() {}

that does nothing, and returns nothing.. Then to "test" my connection, I call this method... If it throws a System.Net.Sockets.Exception, I have lost the connection....

Charles Bretana
Thanks, I guess you validated my suspicion that there was no facility in the framework to give me this functionality. This ping method should work fine. Thanks
Jeremy
+2  A: 

What benefit will you have to check the connection? Even if you ping it, it does not mean that in the next moment the connection will still be alive when you make your remoting call.

Just try/catch you remoting calls and you will know.

This type of checks are meaningless (net connection, file locking, etc.). As the state of the thing you check can change immediately after the check. You just tray, and cleanup/retry if it fails.

Sunny
Because the object I'm remoting is hosted in a windows application. If the application isn't running, I want to be able to restart it.
Jeremy
same thing - if the call fails, restart. as I said, pinging is almost the same, except that it is one more call, as it does not guarantee in any way that the next call will succeed, or that the application will not close before or during the next call.
Sunny