Page 8: Handle Communication Errors |
Communication errors are signaled through two different mechanisms, see the sections below for more information.
This topic contains the following sections:
At the lowest level, communication errors are signaled through the ConsumerTRootConnectionLost event, which can be used as demonstrated in the following method:
AsyncPump.Run( async () => { using (var client = await ConnectAsync("localhost", 9000)) using (var consumer = await Consumer<MyRoot>.CreateAsync(client)) { var connectionLost = new TaskCompletionSource<Exception>(); consumer.ConnectionLost += (s, e) => connectionLost.SetResult(e.Exception); Console.WriteLine("Waiting for the provider to disconnect..."); var exception = await connectionLost.Task; Console.WriteLine("Connection Lost!"); Console.WriteLine("Exception:{0}{1}", exception, Environment.NewLine); } });
We can test this by simply closing the provider, or by running the provider on a different computer and then disconnecting the network cable. In the former case ConnectionLostEventArgsException is null. In the latter case ConnectionLostEventArgsException indicates the reason for the error.
Both ConsumerTRootCreateAsync and ConsumerTRootSendAsync can throw communication-related exceptions, which can be caught and handled as usual.
This concludes the dynamic interface part of the Lawo.EmberPlusSharp.Model namespace tutorial. Proceed to Page 9: Static Interface.