async Task Run() { var stoppingContext = new TransportStoppingContext(Stopping); RetryPolicyContext <TransportStoppingContext> policyContext = _retryPolicy.CreatePolicyContext(stoppingContext); try { while (!IsStopping) { RetryContext <TransportStoppingContext> retryContext = null; try { if (retryContext != null) { LogContext.Warning?.Log(retryContext.Exception, "Retrying {Delay}: {Message}", retryContext.Delay, retryContext.Exception.Message); if (retryContext.Delay.HasValue) { await Task.Delay(retryContext.Delay.Value, Stopping).ConfigureAwait(false); } } if (!IsStopping) { await RunTransport().ConfigureAwait(false); } } catch (OperationCanceledException) { throw; } catch (Exception exception) { if (retryContext != null) { retryContext = retryContext.CanRetry(exception, out RetryContext <TransportStoppingContext> nextRetryContext) ? nextRetryContext : null; } if (retryContext == null && !policyContext.CanRetry(exception, out retryContext)) { break; } } } } catch (Exception exception) { LogContext.Debug?.Log(exception, "ReceiveTransport Run Exception: {InputAddress}", _context.InputAddress); } finally { policyContext.Dispose(); } }
async Task Run() { var stoppingContext = new TransportStoppingContext(Stopping); RetryPolicyContext <TransportStoppingContext> policyContext = _retryPolicy.CreatePolicyContext(stoppingContext); try { RetryContext <TransportStoppingContext> retryContext = null; while (!IsStopping) { try { if (retryContext?.Delay != null) { await Task.Delay(retryContext.Delay.Value, Stopping).ConfigureAwait(false); } if (!IsStopping) { await RunTransport().ConfigureAwait(false); } } catch (OperationCanceledException exception) { if (retryContext == null) { await NotifyFaulted(exception).ConfigureAwait(false); } throw; } catch (Exception exception) { if (retryContext != null) { retryContext = retryContext.CanRetry(exception, out RetryContext <TransportStoppingContext> nextRetryContext) ? nextRetryContext : null; } if (retryContext == null && !policyContext.CanRetry(exception, out retryContext)) { break; } } if (IsStopping) { break; } try { await Task.Delay(TimeSpan.FromSeconds(1), Stopping).ConfigureAwait(false); } catch { // just a little breather before reconnecting the receive transport } } } catch (OperationCanceledException exception) { if (exception.CancellationToken != Stopping) { LogContext.Debug?.Log(exception, "ReceiveTransport Operation Cancelled: {InputAddress}", _context.InputAddress); } } catch (Exception exception) { LogContext.Debug?.Log(exception, "ReceiveTransport Run Exception: {InputAddress}", _context.InputAddress); } finally { policyContext.Dispose(); } }