Add project files.
This commit is contained in:
		| @@ -0,0 +1,65 @@ | ||||
| using Core.Blueprint.External.Clients.Blueprint; | ||||
| using Core.Blueprint.External.GatewayConfigurations; | ||||
| using Lib.Architecture.BuildingBlocks.Helpers.Token; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Refit; | ||||
|  | ||||
| namespace Core.Blueprint.External.ClientConfiguration | ||||
| { | ||||
|     public static class RegisterClientConfiguration | ||||
|     { | ||||
|         public static IServiceCollection RegisterExternalLayer(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             var gatewayConfiguration = new GatewayConfiguration(); | ||||
|             var gatewaySettingsConfiguration = new GatewaySettingsConfigurations(configuration); | ||||
|  | ||||
|             // Register GatewayConfiguration as a singleton | ||||
|             services.AddSingleton(gatewayConfiguration); | ||||
|  | ||||
|             // Register IHttpContextAccessor | ||||
|             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||||
|  | ||||
|             // Register ITokenProvider | ||||
|             services.AddSingleton<ITokenProvider, HttpContextTokenProvider>(); | ||||
|  | ||||
|             // Register the custom AuthenticatedHttpClientHandler | ||||
|             services.AddTransient(provider => | ||||
|             { | ||||
|                 var tokenProvider = provider.GetRequiredService<ITokenProvider>(); | ||||
|                 var trackingIdHandler = new TrackingMechanismExtension(provider.GetRequiredService<IHttpContextAccessor>()); | ||||
|  | ||||
|                 var handler = new AuthenticatedHttpClientHandler(tokenProvider) | ||||
|                 { | ||||
|                     InnerHandler = new HttpClientHandler() // Setting the InnerHandler manually | ||||
|                 }; | ||||
|  | ||||
|                 // Attach the TrackingIdHandler as the outermost handler | ||||
|                 trackingIdHandler.InnerHandler = handler; | ||||
|  | ||||
|                 return handler; | ||||
|             }); | ||||
|  | ||||
|             var blueprintServiceApiUrl = GatewaySettingsConfigurations.GetBlueprintServiceAPIEndpoint().Endpoint.Url; | ||||
|  | ||||
|             // Register IDashBoardServiceClient with the manually created HttpClient | ||||
|             services.AddScoped(provider => | ||||
|             { | ||||
|                 var handler = provider.GetRequiredService<AuthenticatedHttpClientHandler>(); | ||||
|                 var handlerTrackingId = new TrackingMechanismExtension(provider.GetRequiredService<IHttpContextAccessor>()); // Using the TrackingIdHandler here | ||||
|                                                                                                                              // Chain the handlers | ||||
|                 handlerTrackingId.InnerHandler = handler; //chaining | ||||
|  | ||||
|                 var httpClient = new HttpClient(handlerTrackingId) | ||||
|                 { | ||||
|                     BaseAddress = new Uri(blueprintServiceApiUrl), | ||||
|                     Timeout = TimeSpan.FromMinutes(1) | ||||
|                 }; | ||||
|                 return RestService.For<IBlueprintServiceClient>(httpClient); | ||||
|             }); | ||||
|  | ||||
|             return services; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin