'A proper way to handle WCF network related exceptions

In the application I"m working with, there is some communication with the remote service using WCF (basic http binding, no fancy stuff). Since the client is lightweigh, the details of the server are irrelevant, you may assume that there is just a method that always return true (like ping or something).

The proxy is generated using a Task option, the new client instance is created each time the operation is called. Something like this could be spinning inside the timer:

void Foo()
        {
            var client = new PingServiceClient();

            try
            {
                bool result = client.PingAsync().GetAwaiter().GetResult();
            }
            catch
            {
                //log something
            }
            finally
            {
                client.Abort();
            }
        }

My question is, how should I correctly handle the cases when the network is down? Because the behavior is different. I either get an application crashing (I assume on a task finalizer, which is for some reason not handled neither in AppDomain.CurrentDomain.UnhandledException nor in TaskScheduler.UnobservedTaskException), or sometimes it just silently outputs tons of error messages, but not crashing anything. Messages like these:

Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll

I'm struggling to find a graceful way of handling these, so if anybody has some knowledge regarding this please share the approach.

Thanks in advance.

UPD: I have tried to re-create the proxy with Begin/End pair and override the end method implementation in a partial class:

public partial class PingServiceClient : IPingServiceClient, PingService
    {
        public Task<bool> PingSync()
        {
            return Task.Factory.FromAsync(BeginPing(null, null), HandledEndPing);
        }

        private bool HandledEndPing(System.IAsyncResult result)
        {
            var res = false;

            try
            {
                res = EndPing(result);
            }
            catch (Exception e)
            {
                ;
            }

            return res;
        }
    }

Still the same barrage of messages in the output as before (the catch is working, though).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source