From 8afddaec8e68b78b7afbdd08792af616f356b16e Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Thu, 8 Feb 2024 15:20:37 -0500 Subject: [PATCH] Added default headers to httpclient --- .../AppConfigurationRemoteService.cs | 8 +++-- .../Base/AggregateRemoteServiceBase.cs | 32 +++++++++++++++---- .../Localization/LocalizationRemoteService.cs | 8 +++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/ApplicationConfiguration/AppConfigurationRemoteService.cs b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/ApplicationConfiguration/AppConfigurationRemoteService.cs index 233e18c2..14ee74fb 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/ApplicationConfiguration/AppConfigurationRemoteService.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/ApplicationConfiguration/AppConfigurationRemoteService.cs @@ -1,4 +1,5 @@ using EShopOnAbp.WebGateway.Aggregations.Base; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.DependencyInjection; @@ -6,6 +7,9 @@ using Volo.Abp.Json; namespace EShopOnAbp.WebGateway.Aggregations.ApplicationConfiguration; -public class AppConfigurationRemoteService(ILogger> logger, IJsonSerializer jsonSerializer) - : AggregateRemoteServiceBase(logger, jsonSerializer), +public class AppConfigurationRemoteService( + IHttpContextAccessor httpContextAccessor, + IJsonSerializer jsonSerializer, + ILogger> logger) + : AggregateRemoteServiceBase(httpContextAccessor, jsonSerializer, logger), IAppConfigurationRemoteService, ITransientDependency; \ No newline at end of file diff --git a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Base/AggregateRemoteServiceBase.cs b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Base/AggregateRemoteServiceBase.cs index b60cf0e9..f0e3a799 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Base/AggregateRemoteServiceBase.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Base/AggregateRemoteServiceBase.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Volo.Abp.Json; @@ -10,13 +11,16 @@ namespace EShopOnAbp.WebGateway.Aggregations.Base; public abstract class AggregateRemoteServiceBase : IAggregateRemoteService { + private readonly IHttpContextAccessor _httpContextAccessor; private readonly ILogger> _logger; protected IJsonSerializer JsonSerializer { get; } - protected AggregateRemoteServiceBase(ILogger> logger, IJsonSerializer jsonSerializer) + protected AggregateRemoteServiceBase(IHttpContextAccessor httpContextAccessor, IJsonSerializer jsonSerializer, + ILogger> logger) { - _logger = logger; + _httpContextAccessor = httpContextAccessor; JsonSerializer = jsonSerializer; + _logger = logger; } public async Task> GetMultipleAsync( @@ -26,7 +30,7 @@ public abstract class AggregateRemoteServiceBase : IAggregateRemoteService Dictionary> runningTasks = new Dictionary>(); Dictionary completedResult = new Dictionary(); - using (HttpClient httpClient = new HttpClient()) + using (HttpClient httpClient = CreateHttpClient()) { foreach (var service in serviceNameWithUrlDictionary) { @@ -48,11 +52,11 @@ public abstract class AggregateRemoteServiceBase : IAggregateRemoteService completedTasks.Add(completedTask.Key, completedTask.Value); completedResult.Add(completedTask.Key, result); - _logger.LogInformation($"Localization Key: {completedTask.Key}, Value: {result}"); + _logger.LogInformation("Localization Key: {0}, Value: {1}", completedTask.Key, result); } catch (Exception ex) { - _logger.LogInformation($"Error for the {completedTask.Key}: {ex.Message}"); + _logger.LogInformation("Error for the {0}: {1}", completedTask.Key, ex.Message); } } } @@ -60,6 +64,22 @@ public abstract class AggregateRemoteServiceBase : IAggregateRemoteService return completedResult; } + private HttpClient CreateHttpClient() + { + var httpClient = new HttpClient(); + + var headers = _httpContextAccessor.HttpContext?.Request.Headers; + if (headers != null) + { + foreach (var header in headers) + { + httpClient.DefaultRequestHeaders.Add(header.Key, header.Value.ToArray()); + } + } + + return httpClient; + } + public async Task MakeRequestAsync(HttpClient httpClient, string url) { try @@ -72,7 +92,7 @@ public abstract class AggregateRemoteServiceBase : IAggregateRemoteService } catch (Exception e) { - _logger.LogInformation($"Error making request to {url}: {e.Message}"); + _logger.LogInformation("Error making request to {0}: {1}", url, e.Message); throw; } } diff --git a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/LocalizationRemoteService.cs b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/LocalizationRemoteService.cs index 9e582d1f..55db8888 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/LocalizationRemoteService.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/LocalizationRemoteService.cs @@ -1,4 +1,5 @@ using EShopOnAbp.WebGateway.Aggregations.Base; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.DependencyInjection; @@ -9,8 +10,11 @@ namespace EShopOnAbp.WebGateway.Aggregations.Localization; public class LocalizationRemoteService : AggregateRemoteServiceBase, ILocalizationRemoteService, ITransientDependency { - public LocalizationRemoteService(ILogger> logger, IJsonSerializer jsonSerializer) : - base(logger, jsonSerializer) + public LocalizationRemoteService( + IHttpContextAccessor httpContextAccessor, + IJsonSerializer jsonSerializer, + ILogger> logger) + : base(httpContextAccessor, jsonSerializer, logger) { } } \ No newline at end of file