Add project files.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Core.Blueprint.Service.External.Clients;
|
||||
using Core.Blueprint.Service.External.GatewayConfigurations;
|
||||
using Core.Blueprint.Service.External.Helpers.Token;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
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 GatewaySettingsConfiguration(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 handler = new AuthenticatedHttpClientHandler(tokenProvider)
|
||||
{
|
||||
InnerHandler = new HttpClientHandler() // Setting the InnerHandler manually
|
||||
};
|
||||
return handler;
|
||||
});
|
||||
|
||||
var secretServiceApiUrl = GatewaySettingsConfiguration.GetBlueprintApiEndpoint.Endpoint.Uri;
|
||||
|
||||
// Register IBlueprintServiceClient with the manually created HttpClient
|
||||
services.AddScoped<IBlueprintServiceClient>(provider =>
|
||||
{
|
||||
var handler = provider.GetRequiredService<AuthenticatedHttpClientHandler>();
|
||||
var httpClient = new HttpClient(handler)
|
||||
{
|
||||
BaseAddress = secretServiceApiUrl,
|
||||
Timeout = TimeSpan.FromMinutes(1)
|
||||
};
|
||||
return RestService.For<IBlueprintServiceClient>(httpClient);
|
||||
});
|
||||
|
||||
services.AddScoped<IAuthenticationService, AuthenticationService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user