Typed http client with resiliency

This commit is contained in:
2024-09-07 10:45:13 +02:00
parent 05eb37972e
commit 5371485b78
4 changed files with 22 additions and 2 deletions
@@ -3,7 +3,7 @@ using System.ComponentModel;
using System.Core.Extensions;
using System.Extensions;
namespace Net.Maui.Extensions.Extensions;
namespace Net.Maui.Extensions;
public static class ContentPageExtensions
{
@@ -0,0 +1,18 @@
using System.Core.Extensions;
namespace Net.Maui.Extensions.Extensions;
public static class ServiceCollectionExtensions
{
public static IHttpClientBuilder RegisterHttpClient<TService>(this IServiceCollection services)
where TService : class
{
return services.ThrowIfNull()
.AddScoped<IHttpClient<TService>, HttpClient<TService>>(sp =>
{
var factory = sp.GetRequiredService<IHttpClientFactory>();
return new HttpClient<TService>(factory.CreateClient(typeof(TService).Name), false);
})
.AddHttpClient(typeof(TService).Name);
}
}
@@ -20,7 +20,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<Version>0.2.13</Version>
<Version>0.3.0</Version>
<Authors>Alexandru Macocian</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -46,6 +46,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.CorrelationVector" Version="1.0.42" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />