protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); var pkt = new ReadingPacket() { Successful = ReadingStatus.Success, Notes = "This is a nice test" }; for (int i = 0; i < _config.GetValue <int>("Service:Batch"); i++) { pkt.Readings.Add(await _factory.Generate(new Random().Next(1, 10000))); } var result = await Client.AddReadingAsync(pkt); if (result.Success == ReadingStatus.Success) { _logger.LogInformation("Succesfully sent"); } else { _logger.LogInformation("Failed to send"); } await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var counter = 0; var customerId = new Random(); while (!stoppingToken.IsCancellationRequested) { counter++; if (counter % 10 == 0) { Console.WriteLine("Sending Diagnostics.."); var stream = Client.SendDiagnostics(); for (int i = 0; i < 5; i++) { var reading = await _factory.Generate(customerId.Next(1, 10000)); await stream.RequestStream.WriteAsync(reading); } await stream.RequestStream.CompleteAsync(); } _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); var pkt = new ReadingPacket() { Successful = ReadingStatus.Success, Notes = "This is a nice test" }; for (int i = 0; i < _config.GetValue <int>("Service:Batch"); i++) { pkt.Readings.Add(await _factory.Generate(customerId.Next(1, 10000))); } try { var result = await Client.AddReadingAsync(pkt); if (result.Success == ReadingStatus.Success) { _logger.LogInformation("Succesfully sent"); } else { _logger.LogInformation("Failed to send"); } } catch (RpcException ex) { //_logger.LogError($"Exception thrown: {ex}"); // basic status error if (ex.StatusCode == StatusCode.OutOfRange) { _logger.LogError($"{ex.Trailers}"); } _logger.LogError($"Exception thrown: {ex}"); // custom status error } await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken); } }