[en] Handling connection timeout (solved)

Topics: Technical Support
Aug 24, 2011 at 3:41 PM

Hello all

Is there any approach to handling timeouts?

using (var connection = new Zyan.Communication.ZyanConnection(_connectionString, _protocol))
{
...
}

I would assume a try-catch around the above, or how would you approach such a challenge?

Regards

Coordinator
Aug 24, 2011 at 8:22 PM
Edited Aug 24, 2011 at 8:28 PM

Hello cln,

to handle timeouts when connecting to a Zyan server or making remote calls on a existing connection, you can catch SocketException (if you´re using a TCP based protocol).

code example

 

ISomeService proxy;

try
{
    // Connect and login
    var connection = new Zyan.Communication.ZyanConnection(_connectionString, _protocol);
    proxy = connection.CreateProxy<ISomeService>();
}
catch (SocketException ex)
{
    Console.WriteLine(string.Format("Connection failed. The following exception occured: {0}.", ex.Message);
}

...

try
{
    proxy.DoCoolRemoteStuff();
}
catch (SocketException ex)
{
    Console.WriteLine(string.Format("Remote call failed. The following exception occured: {0}.", ex.Message);
}

 

Alternatively you can use the Error event of your ZyanConnection to handle errors while making remote calls.

 

// Register Error event handler for centralized error handling
connection.Error += new EventHandler<ZyanErrorEventArgs>(HandleRpcError);

...

proxy.DoCoolRemoteStuff();

...

private void HandleRpcError(object sender, ZyanErrorEventArgs e)
{
    Console.WriteLine(string.Format("Calling remote member {0} failed because of the following exception: {1}", e.RemoteMemberName, e.Exception.Message);
}

 

 

The Error event fires on any detected exception. Even exceptions, that are not related to network connection! The original exception is provided in the Exception property of the event arguments object. You can check the exception type and then decide which action should be talken. Set the Action property of the event arguments object. The following actions are supported:

  • ThrowException (Throw the exception again)
  • Retry (Retry the request; Please limit your retries by a counter to avoid an endless retry loop)
  • Ignore (Do nothing)

Zyan is also able to detect unexpected disconnections, even when your client idles. Please look here for details, if this feature is interessting for you: http://zyan.codeplex.com/discussions/265268

 

Regards