/// <summary>Reconnects the client.</summary>
        /// <remarks>This method is invoked when the client has disconnected.</remarks>
        /// <exception cref="AggregateException">Aggregate exception containing all exceptions occured over all reconnect attempts.</exception>
        private async void OnClientDisconnected(object sender, EventArgs e)
        {
            this.Config.Log?.LogDebug("Attempting to reconnect, max {Attempts} times. Delay: {Delay}",
                                      this.Config.ReconnectAttempts, this.Config.ReconnectionDelay);

            bool isInfinite = this.Config.ReconnectAttempts < 0;
            ICollection <Exception> exceptions = new List <Exception>(isInfinite ? 0 : this.Config.ReconnectAttempts);

            for (int i = 1; isInfinite || i <= this.Config.ReconnectAttempts; i++)
            {
                try
                {
                    this.Config.CancellationToken.ThrowIfCancellationRequested();
                    this.Config.Log?.LogTrace("Reconnection attempt {Attempt}", i);

                    // wait reconnection delay if any
                    if (this.Config.ReconnectionDelay > TimeSpan.Zero)
                    {
                        await Task.Delay(this.Config.ReconnectionDelay, this.Config.CancellationToken).ConfigureAwait(false);
                    }

                    // attempt to reconnnect unconditionally
                    await _client.ConnectAsync(this.Config.CancellationToken).ConfigureAwait(false);

                    return;
                }
                catch (OperationCanceledException ex)
                {
                    exceptions.Add(ex);
                    break;
                }
                catch (Exception ex) when(
                    (isInfinite && ex.LogAsError(this.Config.Log, "Failed to auto-reconnect")) ||
                    ex.LogAsWarning(this.Config.Log, "Failed to auto-reconnect"))
                {
                    exceptions.Add(ex);
                }
            }

            AggregateException aggrEx = exceptions.Any() ?
                                        new AggregateException("Error(s) occured when trying to automatically reconnect", exceptions) :
                                        new AggregateException("Failed to reconnect, but no exceptions were thrown");

            this.Config.Log?.LogCritical(aggrEx, "Failed to reconnect after {Attempts} attempts", this.Config.ReconnectAttempts > 0 ? this.Config.ReconnectAttempts.ToString() : "Infinite");
            FailedToReconnect?.Invoke(this, new UnhandledExceptionEventArgs(aggrEx, true));
            throw aggrEx;
        }
Beispiel #2
0
        /// <summary>Reconnects the client.</summary>
        /// <remarks>This method is invoked when the client has disconnected.</remarks>
        /// <exception cref="AggregateException">Aggregate exception containing all exceptions occured over all reconnect attempts.</exception>
        private async void OnClientDisconnected(object sender, EventArgs e)
        {
            this.Config.Log?.LogDebug("Attempting to reconnect, max {Attempts} times. Delay: {Delay}",
                                      this.Config.ReconnectAttempts, this.Config.ReconnectionDelay);

            ICollection <Exception> exceptions = new List <Exception>(this.Config.ReconnectAttempts);

            for (int i = 1; i <= this.Config.ReconnectAttempts; i++)
            {
                try
                {
                    this.Config.Log?.LogTrace("Reconnection attempt {Attempt}", i);

                    // wait reconnection delay if any
                    if (this.Config.ReconnectionDelay > TimeSpan.Zero)
                    {
                        await Task.Delay(this.Config.ReconnectionDelay);
                    }

                    // attempt to reconnnect unconditionally
                    await _client.ConnectAsync(this.Config.CancellationToken).ConfigureAwait(false);

                    return;
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                }
            }

            AggregateException aggrEx = exceptions.Any() ?
                                        new AggregateException("Error(s) occured when trying to automatically reconnect", exceptions) :
                                        new AggregateException("Failed to reconnect, but no exceptions were thrown");

            this.Config.Log?.LogError(aggrEx, "Failed to reconnect after {Attempts} attempts", this.Config.ReconnectAttempts);
            FailedToReconnect?.Invoke(this, new UnhandledExceptionEventArgs(aggrEx, true));
            throw aggrEx;
        }