diff --git a/Core/RequestLoggingMiddleware.cs b/Core/RequestLoggingMiddleware.cs index e43b8de..6c21f64 100644 --- a/Core/RequestLoggingMiddleware.cs +++ b/Core/RequestLoggingMiddleware.cs @@ -37,21 +37,32 @@ namespace BMA.EHR.Recruit.Service.Core { using (var client = new HttpClient()) { + // Set timeout to 30 seconds instead of default 100 seconds + client.Timeout = TimeSpan.FromSeconds(30); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", apiKey); - var _res = await client.GetAsync(apiPath); + + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + var _res = await client.GetAsync(apiPath, cts.Token); if (_res.IsSuccessStatusCode) { var _result = await _res.Content.ReadAsStringAsync(); - return _result; } return string.Empty; } } - catch + catch (TaskCanceledException) { - throw; + // Log timeout but don't throw - return empty result instead + Console.WriteLine($"API call timed out: {apiPath}"); + return string.Empty; + } + catch (Exception ex) + { + // Log other exceptions but don't throw - return empty result instead + Console.WriteLine($"API call failed: {apiPath}, Error: {ex.Message}"); + return string.Empty; } } @@ -60,10 +71,10 @@ namespace BMA.EHR.Recruit.Service.Core try { var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}"; - var apiKey = _configuration["API_KEY"]; + var apiKey = _configuration["API_KEY"] ?? ""; var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey); - if (apiResult != null) + if (!string.IsNullOrEmpty(apiResult)) { var raw = JsonConvert.DeserializeObject(apiResult); if (raw != null) @@ -72,9 +83,11 @@ namespace BMA.EHR.Recruit.Service.Core return null; } - catch + catch (Exception ex) { - throw; + // Log exception but don't throw - return null instead + Console.WriteLine($"GetProfileByKeycloakIdAsync failed for {keycloakId}: {ex.Message}"); + return null; } } diff --git a/Program.cs b/Program.cs index 03d40d2..67fee6f 100644 --- a/Program.cs +++ b/Program.cs @@ -67,6 +67,12 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +// Configure HttpClient with timeout +builder.Services.AddHttpClient("default", client => +{ + client.Timeout = TimeSpan.FromSeconds(30); +}); + // use serilog //ConfigureLogs(); //builder.Host.UseSerilog(); @@ -107,10 +113,6 @@ builder.Services.AddSwaggerGen(); builder.Services.ConfigureOptions(); builder.Services.AddHealthChecks(); -// Register Service -builder.Services.AddTransient(); -builder.Services.AddTransient(); - var app = builder.Build(); var apiVersionDescriptionProvider = app.Services.GetRequiredService(); @@ -133,12 +135,11 @@ app.UseHttpsRedirection(); app.UseCors(); app.UseAuthentication(); app.UseAuthorization(); +app.UseMiddleware(); app.UseDefaultFiles(); app.UseStaticFiles(); app.MapControllers(); -app.UseMiddleware(); - // apply migrations await using var scope = app.Services.CreateAsyncScope(); await using var db = scope.ServiceProvider.GetRequiredService();