public override async Task RunAsync(CancellationToken cancellationToken) { using var client = new HttpClient(); while (!cancellationToken.IsCancellationRequested) { await _limiter.WaitAsync(cancellationToken).ConfigureAwait(false); #pragma warning disable CA1303 // Do not pass literals as localized parameters Logger.LogInformation("Connecting with {ControllerUrl}", _options.ControllerUrl.ToString()); try { #if NET using var stream = await client.GetStreamAsync(_options.ControllerUrl, cancellationToken).ConfigureAwait(false); #else using var stream = await client.GetStreamAsync(_options.ControllerUrl).ConfigureAwait(false); #endif using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true); using var cancellation = cancellationToken.Register(stream.Close); while (true) { var json = await reader.ReadLineAsync().ConfigureAwait(false); if (string.IsNullOrEmpty(json)) { break; } var message = System.Text.Json.JsonSerializer.Deserialize <Message>(json); Logger.LogInformation("Received {MessageType} for {MessageKey}", message.MessageType, message.Key); Logger.LogInformation(json); Logger.LogInformation(message.MessageType.ToString()); if (message.MessageType == MessageType.Update) { _proxyConfigProvider.Update(message.Routes, message.Cluster); } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { Logger.LogInformation("Stream ended: {Message}", ex.Message); } #pragma warning restore CA1303 // Do not pass literals as localized parameters } }
public override async Task RunAsync(CancellationToken cancellationToken) { using var client = new HttpClient(); while (!cancellationToken.IsCancellationRequested) { await _limiter.WaitAsync(cancellationToken).ConfigureAwait(false); Logger.LogInformation($"Connecting with { _options.ControllerUrl.ToString()}"); try { using var stream = await client.GetStreamAsync(_options.ControllerUrl, cancellationToken).ConfigureAwait(false); using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true); using var cancellation = cancellationToken.Register(stream.Close); while (true) { var json = await reader.ReadLineAsync().ConfigureAwait(false); if (string.IsNullOrEmpty(json)) { break; } var message = JsonSerializer.Deserialize <Message>(json); Logger.LogInformation($"Received {message.MessageType} for { message.Key}"); Logger.LogInformation(json); Logger.LogInformation(message.MessageType.ToString()); if (message.MessageType == MessageType.Update) { _proxyConfigProvider.Update(message.Routes, message.Cluster); } } } catch (Exception ex) { Logger.LogInformation($"Stream ended: { ex.Message}"); } } }