Skip to content

Commit 5e71c23

Browse files
Update Polly and support IConcurrentPolicyRegistry<string> (#31708)
1 parent 5ba5e56 commit 5e71c23

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
<NSwagApiDescriptionClientVersion>13.0.4</NSwagApiDescriptionClientVersion>
247247
<PlaywrightSharpVersion>0.192.0</PlaywrightSharpVersion>
248248
<PollyExtensionsHttpVersion>3.0.0</PollyExtensionsHttpVersion>
249-
<PollyVersion>7.1.0</PollyVersion>
249+
<PollyVersion>7.2.2</PollyVersion>
250250
<SeleniumSupportVersion>4.0.0-beta1</SeleniumSupportVersion>
251251
<SeleniumWebDriverChromeDriverVersion>89.0.4389.2300-beta</SeleniumWebDriverChromeDriverVersion>
252252
<SeleniumWebDriverVersion>4.0.0-beta1</SeleniumWebDriverVersion>

src/HttpClientFactory/Polly/src/DependencyInjection/PollyServiceCollectionExtensions.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -8,15 +8,15 @@
88
namespace Microsoft.Extensions.DependencyInjection
99
{
1010
/// <summary>
11-
/// Provides convenience extension methods to register <see cref="IPolicyRegistry{String}"/> and
12-
/// <see cref="IReadOnlyPolicyRegistry{String}"/> in the service collection.
11+
/// Provides convenience extension methods to register <see cref="IPolicyRegistry{String}"/> and
12+
/// <see cref="IReadOnlyPolicyRegistry{String}"/> in the service collection.
1313
/// </summary>
1414
public static class PollyServiceCollectionExtensions
1515
{
1616
/// <summary>
1717
/// Registers an empty <see cref="PolicyRegistry"/> in the service collection with service types
18-
/// <see cref="IPolicyRegistry{String}"/>, and <see cref="IReadOnlyPolicyRegistry{String}"/> and returns
19-
/// the newly created registry.
18+
/// <see cref="IPolicyRegistry{String}"/>, <see cref="IReadOnlyPolicyRegistry{String}"/>, and
19+
/// <see cref="IConcurrentPolicyRegistry{String}"/> and returns the newly created registry.
2020
/// </summary>
2121
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
2222
/// <returns>The newly created <see cref="IPolicyRegistry{String}"/>.</returns>
@@ -30,6 +30,8 @@ public static IPolicyRegistry<string> AddPolicyRegistry(this IServiceCollection
3030
// Create an empty registry, register and return it as an instance. This is the best way to get a
3131
// single instance registered using both interfaces.
3232
var registry = new PolicyRegistry();
33+
34+
services.AddSingleton<IConcurrentPolicyRegistry<string>>(registry);
3335
services.AddSingleton<IPolicyRegistry<string>>(registry);
3436
services.AddSingleton<IReadOnlyPolicyRegistry<string>>(registry);
3537

@@ -38,8 +40,8 @@ public static IPolicyRegistry<string> AddPolicyRegistry(this IServiceCollection
3840

3941
/// <summary>
4042
/// Registers the provided <see cref="IPolicyRegistry{String}"/> in the service collection with service types
41-
/// <see cref="IPolicyRegistry{String}"/>, and <see cref="IReadOnlyPolicyRegistry{String}"/> and returns
42-
/// the provided registry.
43+
/// <see cref="IPolicyRegistry{String}"/>, <see cref="IReadOnlyPolicyRegistry{String}"/>, and
44+
/// <see cref="IConcurrentPolicyRegistry{String}"/> and returns the provided registry.
4345
/// </summary>
4446
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
4547
/// <param name="registry">The <see cref="IPolicyRegistry{String}"/>.</param>
@@ -59,13 +61,18 @@ public static IPolicyRegistry<string> AddPolicyRegistry(this IServiceCollection
5961
services.AddSingleton<IPolicyRegistry<string>>(registry);
6062
services.AddSingleton<IReadOnlyPolicyRegistry<string>>(registry);
6163

64+
if (registry is IConcurrentPolicyRegistry<string> concurrentRegistry)
65+
{
66+
services.AddSingleton<IConcurrentPolicyRegistry<string>>(concurrentRegistry);
67+
}
68+
6269
return registry;
6370
}
6471

6572
/// <summary>
6673
/// Registers an empty <see cref="PolicyRegistry"/> in the service collection with service types
67-
/// <see cref="IPolicyRegistry{String}"/>, and <see cref="IReadOnlyPolicyRegistry{String}"/> and
68-
/// uses the specified delegate to configure it.
74+
/// <see cref="IPolicyRegistry{String}"/>, <see cref="IReadOnlyPolicyRegistry{String}"/>, and
75+
/// <see cref="IConcurrentPolicyRegistry{String}"/> and uses the specified delegate to configure it.
6976
/// </summary>
7077
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
7178
/// <param name="configureRegistry">A delegate that is used to configure an <see cref="IPolicyRegistry{String}"/>.</param>
@@ -93,6 +100,7 @@ public static IServiceCollection AddPolicyRegistry(this IServiceCollection servi
93100
return registry;
94101
});
95102

103+
services.AddSingleton<IConcurrentPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>());
96104
services.AddSingleton<IPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>());
97105
services.AddSingleton<IReadOnlyPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>());
98106

src/HttpClientFactory/Polly/test/DependencyInjection/PollyHttpClientBuilderExtensionsTest.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Extensions.Http;
1212
using Microsoft.Extensions.Http.Logging;
1313
using Polly;
14+
using Polly.Registry;
1415
using Xunit;
1516

1617
namespace Microsoft.Extensions.DependencyInjection
@@ -468,6 +469,60 @@ public async Task AddPolicyHandlerFromRegistry_WithConfigureDelegate_AddsPolicyH
468469
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
469470
}
470471

472+
[Fact]
473+
public void AddPolicyHandlerFromRegistry_WithoutConfigureDelegate_AddsPolicyRegistries()
474+
{
475+
var serviceCollection = new ServiceCollection();
476+
477+
// Act
478+
serviceCollection.AddPolicyRegistry();
479+
480+
var services = serviceCollection.BuildServiceProvider();
481+
var registry = services.GetService<IPolicyRegistry<string>>();
482+
483+
// Assert
484+
Assert.NotNull(registry);
485+
Assert.Same(registry, services.GetService<IConcurrentPolicyRegistry<string>>());
486+
Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
487+
}
488+
489+
[Fact]
490+
public void AddPolicyHandlerFromRegistry_WithRegistry_AddsPolicyRegistries()
491+
{
492+
var serviceCollection = new ServiceCollection();
493+
var registry = new PolicyRegistry();
494+
495+
// Act
496+
serviceCollection.AddPolicyRegistry(registry);
497+
498+
var services = serviceCollection.BuildServiceProvider();
499+
500+
// Assert
501+
Assert.Same(registry, services.GetService<IConcurrentPolicyRegistry<string>>());
502+
Assert.Same(registry, services.GetService<IPolicyRegistry<string>>());
503+
Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
504+
}
505+
506+
[Fact]
507+
public void AddPolicyHandlerFromRegistry_WithConfigureDelegate_AddsPolicyRegistries()
508+
{
509+
var serviceCollection = new ServiceCollection();
510+
511+
// Act
512+
serviceCollection.AddPolicyRegistry((serviceProvider, registry) =>
513+
{
514+
// No-op
515+
});
516+
517+
var services = serviceCollection.BuildServiceProvider();
518+
var registry = services.GetService<IPolicyRegistry<string>>();
519+
520+
// Assert
521+
Assert.NotNull(registry);
522+
Assert.Same(registry, services.GetService<IConcurrentPolicyRegistry<string>>());
523+
Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
524+
}
525+
471526
// Throws an exception or fails on even numbered requests, otherwise succeeds.
472527
private class FaultyMessageHandler : DelegatingHandler
473528
{

0 commit comments

Comments
 (0)