Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ResilienceHandlerContext.OnPipelineDisposed #4497

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@
{
"Member": "void Microsoft.Extensions.Http.Resilience.ResilienceHandlerContext.EnableReloads<TOptions>(string? name = null);",
"Stage": "Stable"
},
{
"Member": "TOptions Microsoft.Extensions.Http.Resilience.ResilienceHandlerContext.GetOptions<TOptions>(string name);",
"Stage": "Stable"
},
{
"Member": "void Microsoft.Extensions.Http.Resilience.ResilienceHandlerContext.OnPipelineDisposed(System.Action callback);",
"Stage": "Stable"
}
],
"Properties": [
Expand Down Expand Up @@ -530,4 +538,4 @@
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Microsoft.Extensions.Http.Resilience.Internal;
using Microsoft.Shared.Diagnostics;
using Polly.DependencyInjection;

namespace Microsoft.Extensions.Http.Resilience;
Expand Down Expand Up @@ -48,5 +49,20 @@ internal ResilienceHandlerContext(AddResiliencePipelineContext<HttpKey> context)
/// </remarks>
public void EnableReloads<TOptions>(string? name = null) => _context.EnableReloads<TOptions>(name);

internal T GetOptions<T>(string name) => _context.GetOptions<T>(name);
/// <summary>
/// Gets the options identified by <paramref name="name"/>.
/// </summary>
/// <typeparam name="TOptions">The options type.</typeparam>
/// <param name="name">The options name, if any.</param>
/// <returns>The options instance.</returns>
/// <remarks>
/// If <paramref name="name"/> is <see langword="null"/> then the global options are returned.
/// </remarks>
public TOptions GetOptions<TOptions>(string name) => _context.GetOptions<TOptions>(name);

/// <summary>
/// Registers a callback that is called when the pipeline instance being configured is disposed.
/// </summary>
/// <param name="callback">The callback delegate.</param>
public void OnPipelineDisposed(Action callback) => _context.OnPipelineDisposed(Throw.IfNull(callback));
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ public void AddResilienceHandler_EnsureCorrectServicesRegistered()
Assert.Contains(services, s => s.ServiceType == typeof(ResiliencePipelineProvider<HttpKey>));
}

[Fact]
public async Task AddResilienceHandler_OnPipelineDisposed_EnsureCalled()
{
var onPipelineDisposedCalled = false;
var services = new ServiceCollection();
IHttpClientBuilder? builder = services
.AddHttpClient("client")
.ConfigurePrimaryHttpMessageHandler(() => new TestHandlerStub(HttpStatusCode.OK));

builder.AddResilienceHandler("test", (builder, context) =>
{
builder.AddTimeout(TimeSpan.FromSeconds(1));
context.OnPipelineDisposed(() => onPipelineDisposedCalled = true);
});

var serviceProvider = services.BuildServiceProvider();

var client = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient("client");
using var request = new HttpRequestMessage(HttpMethod.Get, "https://dummy");

await client.GetStringAsync("https://dummy");
serviceProvider.Dispose();

onPipelineDisposedCalled.Should().BeTrue();
}

[Fact]
public void AddResilienceHandler_EnsureServicesNotAddedTwice()
{
Expand Down
Loading