public void PerformSynchronization(object sender, System.Timers.ElapsedEventArgs e) { try { _logger.LogInformation("PerformSynchronization() was called..."); if (_isSynchronizationActive) { // do nothing if a sync is already in progress. _logger.LogInformation("Sync is in progress - won't start a new one."); return; } var syncBundlesToRun = _syncStrategyBundles.Where(bundle => bundle.IsTimeToExecute(DateTime.Now)); // for every '_minuteIntervalBetweenEsasWsHealthcheck' minutes, do a health-check if (DateTime.Now.Minute % _minuteIntervalBetweenEsasWsHealthcheck == 0) { _logger.LogInformation($"Running an ESAS WS-Healthcheck..."); EsasWebServiceHealthCheck healthCheck = _esasWebServiceHealthChecker.PerformHealthCheck(); var dbContext = _esasStagingDbContextFactory.CreateDbContext(); using (dbContext) { dbContext.HealthChecks.Add(healthCheck); dbContext.SaveChanges(); } _logger.LogInformation($"ESAS WS-Healthcheck reported status {healthCheck.HttpStatusCode}."); if (healthCheck.HttpStatusCode != "200") { Exception healthCheckException = new Exception(healthCheck.Message); _logger.LogError(healthCheckException.Message, healthCheckException); return; } } if (syncBundlesToRun.Any()) { _isSynchronizationActive = true; executeSyncBundles(syncBundlesToRun); _isSynchronizationActive = false; } } catch (Exception ex) { // Allow the service to continue, but do log the error. _logger.LogError("ERROR: " + ex.Message, ex); if (ex.InnerException != null) { _logger.LogError("ERROR: " + ex.InnerException.Message, ex.InnerException); } _isSynchronizationActive = false; } }
public EsasWebServiceHealthCheck PerformHealthCheck() { _logger.LogInformation("Performing health-check..."); string wsAliveUri = string.Join(@"/", _esasWsUri, IS_ALIVE_ENDPOINT); EsasWebServiceHealthCheck healthCheck = new EsasWebServiceHealthCheck { CheckTime = DateTime.Now }; Stopwatch sw = new Stopwatch(); sw.Start(); try { HttpWebRequest isAliveWebRequest = (HttpWebRequest)HttpWebRequest.Create(wsAliveUri); string esasIntegrationUserName = ConfigurationManager.AppSettings["EsasIntegrationUserName"]; string esasIntegrationPassword = ConfigurationManager.AppSettings["EsasIntegrationPassword"]; string esasIntegrationDomain = ConfigurationManager.AppSettings["EsasIntegrationDomain"]; isAliveWebRequest.Credentials = new NetworkCredential(esasIntegrationUserName, esasIntegrationPassword, esasIntegrationDomain); // Importér fra PFX/P12, hvis en sådan findes: X509Certificate2 certificate = new X509Certificate2(); certificate.Import(_esasSecuritySettings.CertificatePath, _esasSecuritySettings.CertificatePassword, X509KeyStorageFlags.DefaultKeySet); if (certificate == null) { _logger.LogError("Could not add ESAS certificate!"); throw new Exception("Could not add ESAS certificate!"); } isAliveWebRequest.ClientCertificates.Add(certificate); var response = isAliveWebRequest.GetResponse(); HttpStatusCode status = (response as HttpWebResponse).StatusCode; healthCheck.HttpStatusCode = ((int)status).ToString(); StreamReader sr = new StreamReader(response.GetResponseStream()); using (sr) { healthCheck.Message = sr.ReadToEnd(); } } catch (WebException ex) { HttpStatusCode status = (ex.Response as HttpWebResponse).StatusCode; healthCheck.HttpStatusCode = ((int)status).ToString(); healthCheck.Message = ex.Message; } catch (Exception ex) { string errorMessage = $"Der opstod en fejl i forsøget på at kontakte ESAS web-servicen. Fejlen var {ex.Message}"; healthCheck.HttpStatusCode = "500"; healthCheck.Message = errorMessage; _logger.LogError(errorMessage); } sw.Stop(); healthCheck.CheckTimeMs = sw.ElapsedMilliseconds; return(healthCheck); }