#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Core.Services.UI;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using System;
using System.Net;
using System.Net.Http;
#endregion
namespace Lumina.Presentation.Web.Common.DependencyInjection;
///
/// Contains all services of the Presentation Web layer.
///
public static class PresentationWebLayerServices
{
#region ===================================================================== METHODS ===================================================================================
///
/// Registers the services of the Presentation Web layer into the dependency injection container.
///
/// The service collection to add the services to.
/// The updated .
public static IServiceCollection AddPresentationWebLayerServices(this IServiceCollection services)
{
// scan the current assembly for validators and register them to the DI container
services.AddValidatorsFromAssemblyContaining(ServiceLifetime.Singleton);
// handle transient errors like network timeouts or intermittent failures
var retryPolicy = Policy
.Handle()
.OrResult(r => !r.IsSuccessStatusCode)
.WaitAndRetryAsync(3, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
// use a circuit breaker to prevent repeatedly calling a failing service
var circuitBreakerPolicy = Policy
.Handle()
.OrResult(r => r.StatusCode == HttpStatusCode.InternalServerError)
.CircuitBreakerAsync(3, TimeSpan.FromSeconds(30));
var policy = Policy.WrapAsync(retryPolicy, circuitBreakerPolicy);
// register the typed client used for the API interaction
services.AddHttpClient()
.AddPolicyHandler(retryPolicy)
.AddPolicyHandler(circuitBreakerPolicy);
services.AddSingleton();
return services;
}
#endregion
}