public AppCallerResponse CallApp(string url) { AppCallerResponse response = new AppCallerResponse(); try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(url); var responseService = client.GetAsync(client.BaseAddress); responseService.Wait(); var result = responseService.Result; if (result.IsSuccessStatusCode) { result.Content.ReadAsStringAsync(); int responseCode = (int)result.StatusCode; if (responseCode > 199 && responseCode < 299) { response.HasNotification = true; response.status = Model.Enums.StatusEnum.UP; response.ResponseMessage = responseCode.ToString(); } } else { response.HasNotification = true; response.status = Model.Enums.StatusEnum.DOWN; } } } catch (Exception ex) { _logService.InsertLog(new Data.Entities.Log() { Exception = ex.Message, Request = url }); } return(response); }
public virtual void DoWork(CancellationToken cancellationToken) { try { object lockObject = new object(); lock (lockObject) { List <App> apps = _appService.GetAll(); foreach (var app in apps) { AppCallerResponse response = _appCallerService.CallApp(app.Url); if (response.HasNotification) { Notification notification = _notificationService.GetNotification(); if (notification == null) { continue; } app.StatusId = (int)response.status; _appService.Update(app); _notificationSender.Send(notification.NotificationValue, "notificaiton subject", "status is " + response.status.ToString() + " responseCode is " + response.ResponseMessage); } } } } catch (Exception ex) { _logService.InsertLog(new Data.Entities.Log() { Exception = ex.Message }); } }