From c39103c365ee57dbc14624b1092c6c0946d53242 Mon Sep 17 00:00:00 2001 From: Martin Taillefer Date: Sat, 6 Jan 2024 06:52:41 -0800 Subject: [PATCH] Expose the ResilienceHandler type. Fixes #4759 --- ...icrosoft.Extensions.Http.Resilience.csproj | 1 + .../{Internal => }/ResilienceHandler.cs | 39 ++++++++++++++++--- .../Resilience/ResilienceHandlerTest.cs | 2 +- 3 files changed, 36 insertions(+), 6 deletions(-) rename src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/{Internal => }/ResilienceHandler.cs (64%) diff --git a/src/Libraries/Microsoft.Extensions.Http.Resilience/Microsoft.Extensions.Http.Resilience.csproj b/src/Libraries/Microsoft.Extensions.Http.Resilience/Microsoft.Extensions.Http.Resilience.csproj index 26ad109d592..ac92ad2d833 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Resilience/Microsoft.Extensions.Http.Resilience.csproj +++ b/src/Libraries/Microsoft.Extensions.Http.Resilience/Microsoft.Extensions.Http.Resilience.csproj @@ -15,6 +15,7 @@ true true true + true diff --git a/src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/Internal/ResilienceHandler.cs b/src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/ResilienceHandler.cs similarity index 64% rename from src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/Internal/ResilienceHandler.cs rename to src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/ResilienceHandler.cs index 5e979cf835c..a79d62e0975 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/Internal/ResilienceHandler.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Resilience/Resilience/ResilienceHandler.cs @@ -2,29 +2,58 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Http.Diagnostics; +using Microsoft.Extensions.Http.Resilience.Internal; +using Microsoft.Shared.DiagnosticIds; +using Microsoft.Shared.Diagnostics; using Polly; -namespace Microsoft.Extensions.Http.Resilience.Internal; +namespace Microsoft.Extensions.Http.Resilience; /// -/// Base class for resilience handler, i.e. handlers that use resilience strategies to send the requests. +/// Base class for resilience handler, i.e. handlers that use resilience strategies to send the requests. /// -internal sealed class ResilienceHandler : DelegatingHandler +[Experimental(diagnosticId: DiagnosticIds.Experiments.Resilience, UrlFormat = DiagnosticIds.UrlFormat)] +public class ResilienceHandler : DelegatingHandler { private readonly Func> _pipelineProvider; + /// + /// Initializes a new instance of the class. + /// + /// The pipeline provider that supplies pipelines in response to an http message. + /// If is . public ResilienceHandler(Func> pipelineProvider) { - _pipelineProvider = pipelineProvider; + _pipelineProvider = Throw.IfNull(pipelineProvider); } - /// + /// + /// Initializes a new instance of the class. + /// + /// The pipeline to use for the message. + /// If is . + public ResilienceHandler(ResiliencePipeline pipeline) + { + _ = Throw.IfNull(pipeline); + _pipelineProvider = _ => pipeline; + } + + /// + /// Sends an HTTP request to the inner handler to send to the server as an asynchronous operation. + /// + /// The HTTP request message to send to the server. + /// A cancellation token to cancel operation. + /// The task object representing the asynchronous operation. + /// If is . protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + _ = Throw.IfNull(request); + var pipeline = _pipelineProvider(request); var created = false; if (request.GetResilienceContext() is not ResilienceContext context) diff --git a/test/Libraries/Microsoft.Extensions.Http.Resilience.Tests/Resilience/ResilienceHandlerTest.cs b/test/Libraries/Microsoft.Extensions.Http.Resilience.Tests/Resilience/ResilienceHandlerTest.cs index 19413422148..45840010e87 100644 --- a/test/Libraries/Microsoft.Extensions.Http.Resilience.Tests/Resilience/ResilienceHandlerTest.cs +++ b/test/Libraries/Microsoft.Extensions.Http.Resilience.Tests/Resilience/ResilienceHandlerTest.cs @@ -22,7 +22,7 @@ public class ResilienceHandlerTest [Theory] public async Task SendAsync_EnsureRequestMetadataFlows(bool resilienceContextSet) { - using var handler = new ResilienceHandler(_ => ResiliencePipeline.Empty); + using var handler = new ResilienceHandler(ResiliencePipeline.Empty); using var invoker = new HttpMessageInvoker(handler); using var request = new HttpRequestMessage();