public void Dispose() { subscriptionThread?.Abort(); subscriptionThread = null; if (subscriptionListener != null) { subscriptionListener.ServerEventReceived -= _eventHandler.Invoke; subscriptionListener.Dispose(); subscriptionListener = null; } subscription?.Dispose(); subscription = null; workingTaskCancellationTokenSource?.Cancel(); workingTaskCancellationTokenSource?.Dispose(); workingTaskCancellationTokenSource = null; isListening = false; }
private async void HoldSubscription() { while (workingTaskCancellationTokenSource != null && !workingTaskCancellationTokenSource.Token.IsCancellationRequested) { try { subscription = _subscriptionFactory.Invoke(); subscriptionListener = new ServerToClientStreamListener <TReceivedEvent>(subscription); subscriptionListener.ServerEventReceived += _eventHandler.Invoke; _logger?.LogDebug($"Subscription of type {nameof(TReceivedEvent)} prepared. Beginning to listen..."); await subscriptionListener.ListenAsync(); } catch (RpcException) { _logger?.LogWarning($"Encountered a RpcException while trying to configure the {nameof(TReceivedEvent)} subscription. Retrying in {retryTimeoutMilliseconds / 1000} seconds..."); await Task.Delay(retryTimeoutMilliseconds); } catch (Exception ex) { _logger?.LogError($"Encountered an unexpected exception while trying to configure the {nameof(TReceivedEvent)} subscription: {ex} ({ex.Message}). Retrying in {retryTimeoutMilliseconds / 1000} seconds..."); await Task.Delay(retryTimeoutMilliseconds); } } }