Click or drag to resize
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:

The ConnectionLost Event

At the lowest level, communication errors are signaled through the ConsumerTRootConnectionLost event, which can be used as demonstrated in the following method:

C#
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.

Exceptions