Exemple #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                _logger.LogInformation($"API found on {_serviceBaseUrl}");
                var baseUri       = new Uri($"http://{_serviceBaseUrl}:{_serviceBasePort}");
                var serviceClient = new CurrentIpApiClient(baseUri);
                var ipAddress     = await GetPublicIPAsync(stoppingToken).ConfigureAwait(false);

                var report = new Report {
                    CurrentIP   = ipAddress,
                    MachineName = Environment.MachineName,
                    MachineTag  = "dev-win-10"
                };
                _logger.LogInformation($"Going to log ip {report.CurrentIP} for {report.MachineName} (tagged as {report.MachineTag}) to {baseUri}");
                try {
                    var status = await serviceClient.SubmitReportAsync(report, stoppingToken).ConfigureAwait(false);

                    _logger.LogInformation($"Submitted report to {baseUri} with success status {status}");
                }
                catch (Exception e) {
                    _logger.LogError(e, "Failed to publish a report");
                }
                await Task.Delay(TimeSpan.FromSeconds(_timeOut), stoppingToken);
            }
        }
        public async Task Post_Report_Returns_Ok()
        {
            var report = new Report {
                CurrentIP   = "10.10.10.1",
                MachineName = "abadsb/abab",
                MachineTag  = "machine-tag"
            };
            var result = await _currentIpApiClient.SubmitReportAsync(report, CancellationToken.None);

            result.ShouldBeTrue();
        }