Skip to content

Commit

Permalink
Use Microsoft.Extensions.Http.Resilience
Browse files Browse the repository at this point in the history
Use the Microsoft.Extensions.Http.Resilience library.
  • Loading branch information
martincostello committed Nov 6, 2023
1 parent 5dc4c38 commit b1ac287
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 81 deletions.
7 changes: 5 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup>
<AspNetSecurityOAuthVersion>7.0.4</AspNetSecurityOAuthVersion>
<PollyVersion>8.1.0</PollyVersion>
<SwashbuckleAspNetCoreVersion>6.5.0</SwashbuckleAspNetCoreVersion>
</PropertyGroup>
<ItemGroup>
Expand All @@ -24,12 +25,14 @@
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0-rc.2.23480.2" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.36.0" />
<PackageVersion Include="Microsoft.DotNet.XliffTasks" Version="1.0.0-beta.21452.1" />
<PackageVersion Include="Microsoft.Extensions.Http.Polly" Version="8.0.0-rc.2.23480.2" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0-rc.2.23510.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.Playwright" Version="1.39.0" />
<PackageVersion Include="Microsoft.TypeScript.MSBuild" Version="5.2.2" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="Polly" Version="8.1.0" />
<PackageVersion Include="Polly.Core" Version="$(PollyVersion)" />
<PackageVersion Include="Polly.Extensions" Version="$(PollyVersion)" />
<PackageVersion Include="Polly.RateLimiting" Version="$(PollyVersion)" />
<PackageVersion Include="Refit" Version="7.0.0" />
<PackageVersion Include="ReportGenerator" Version="5.1.26" />
<PackageVersion Include="Serilog" Version="3.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MartinCostello.LondonTravel.Site.Options;
using MartinCostello.LondonTravel.Site.Services.Tfl;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Options;
using Refit;

Expand All @@ -25,7 +26,8 @@ public static IServiceCollection AddHttpClients(this IServiceCollection services
{
services
.AddHttpClient(Microsoft.Extensions.Options.Options.DefaultName)
.ApplyDefaultConfiguration();
.ApplyDefaultConfiguration()
.AddStandardResilienceHandler();

using var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<SiteOptions>();
Expand All @@ -37,14 +39,16 @@ public static IServiceCollection AddHttpClients(this IServiceCollection services
services
.AddHttpClient(providerName)
.ApplyDefaultConfiguration()
.ApplyRemoteAuthenticationConfiguration();
.ApplyRemoteAuthenticationConfiguration()
.AddStandardResilienceHandler();
}
}

services
.AddHttpClient(nameof(ITflClient))
.AddTypedClient(AddTfl)
.ApplyDefaultConfiguration();
.ApplyDefaultConfiguration()
.AddStandardResilienceHandler();

services.AddSingleton<IHttpContentSerializer>((p) =>
{
Expand Down
21 changes: 1 addition & 20 deletions src/LondonTravel.Site/Extensions/IHttpClientBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Net;
using System.Net.Http.Headers;
using System.Reflection;
using Polly;
using Polly.Registry;

namespace MartinCostello.LondonTravel.Site.Extensions;

Expand Down Expand Up @@ -34,8 +32,7 @@ public static IHttpClientBuilder ApplyDefaultConfiguration(this IHttpClientBuild
return builder
.ConfigurePrimaryHttpMessageHandler(CreatePrimaryHttpHandler)
.ConfigureHttpClient(ApplyDefaultConfiguration)
.AddHttpMessageHandler<CorrelationIdHandler>()
.AddPolicyHandlerFromRegistry(GetRequestPolicy);
.AddHttpMessageHandler<CorrelationIdHandler>();
}

/// <summary>
Expand Down Expand Up @@ -75,22 +72,6 @@ private static void ApplyRemoteAuthenticationConfiguration(HttpClient client)
client.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB
}

/// <summary>
/// Creates a policy to use for an HTTP request.
/// </summary>
/// <param name="registry">The policy registry to use.</param>
/// <param name="request">The HTTP request to get the policy for.</param>
/// <returns>
/// The policy to use for <paramref name="request"/>.
/// </returns>
private static IAsyncPolicy<HttpResponseMessage> GetRequestPolicy(
IReadOnlyPolicyRegistry<string> registry,
HttpRequestMessage request)
{
string policyName = request.Method == HttpMethod.Get ? "ReadPolicy" : "WritePolicy";
return registry.Get<IAsyncPolicy<HttpResponseMessage>>(policyName);
}

/// <summary>
/// Creates the primary HTTP message handler to use for all requests.
/// </summary>
Expand Down

This file was deleted.

8 changes: 5 additions & 3 deletions src/LondonTravel.Site/LondonTravel.Site.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CA1716</NoWarn>
<NoWarn>$(NoWarn);CA1716;EXTEXP0003</NoWarn>
<OutputType>Exe</OutputType>
<PreserveCompilationContext>true</PreserveCompilationContext>
<RootNamespace>MartinCostello.LondonTravel.Site</RootNamespace>
Expand Down Expand Up @@ -33,9 +33,11 @@
<PackageReference Include="Microsoft.AspNetCore.AzureAppServices.HostingStartup" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="Microsoft.DotNet.XliffTasks" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" />
<PackageReference Include="Polly" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="Polly.Extensions" />
<PackageReference Include="Polly.RateLimiting" />
<PackageReference Include="Refit" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Logging" />
Expand Down
1 change: 0 additions & 1 deletion src/LondonTravel.Site/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@
builder.Services.AddTransient((p) => p.GetRequiredService<IOptionsMonitor<SiteOptions>>().CurrentValue.Authentication!.UserStore!);
builder.Services.AddTransient((p) => p.GetRequiredService<IOptionsMonitor<SiteOptions>>().CurrentValue.Tfl!);

builder.Services.AddPolly();
builder.Services.AddHttpClients();

builder.Services.AddApplicationAuthentication(builder.Configuration);
Expand Down

0 comments on commit b1ac287

Please sign in to comment.