Skip to content
代码片段 群组 项目
未验证 提交 5e71c23a 编辑于 作者: Martin Costello's avatar Martin Costello 提交者: GitHub
浏览文件

Update Polly and support IConcurrentPolicyRegistry<string> (#31708)

上级 5ba5e56f
No related branches found
No related tags found
无相关合并请求
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
<NSwagApiDescriptionClientVersion>13.0.4</NSwagApiDescriptionClientVersion> <NSwagApiDescriptionClientVersion>13.0.4</NSwagApiDescriptionClientVersion>
<PlaywrightSharpVersion>0.192.0</PlaywrightSharpVersion> <PlaywrightSharpVersion>0.192.0</PlaywrightSharpVersion>
<PollyExtensionsHttpVersion>3.0.0</PollyExtensionsHttpVersion> <PollyExtensionsHttpVersion>3.0.0</PollyExtensionsHttpVersion>
<PollyVersion>7.1.0</PollyVersion> <PollyVersion>7.2.2</PollyVersion>
<SeleniumSupportVersion>4.0.0-beta1</SeleniumSupportVersion> <SeleniumSupportVersion>4.0.0-beta1</SeleniumSupportVersion>
<SeleniumWebDriverChromeDriverVersion>89.0.4389.2300-beta</SeleniumWebDriverChromeDriverVersion> <SeleniumWebDriverChromeDriverVersion>89.0.4389.2300-beta</SeleniumWebDriverChromeDriverVersion>
<SeleniumWebDriverVersion>4.0.0-beta1</SeleniumWebDriverVersion> <SeleniumWebDriverVersion>4.0.0-beta1</SeleniumWebDriverVersion>
......
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
...@@ -8,15 +8,15 @@ using Polly.Registry; ...@@ -8,15 +8,15 @@ using Polly.Registry;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
/// <summary> /// <summary>
/// Provides convenience extension methods to register <see cref="IPolicyRegistry{String}"/> and /// Provides convenience extension methods to register <see cref="IPolicyRegistry{String}"/> and
/// <see cref="IReadOnlyPolicyRegistry{String}"/> in the service collection. /// <see cref="IReadOnlyPolicyRegistry{String}"/> in the service collection.
/// </summary> /// </summary>
public static class PollyServiceCollectionExtensions public static class PollyServiceCollectionExtensions
{ {
/// <summary> /// <summary>
/// Registers an empty <see cref="PolicyRegistry"/> in the service collection with service types /// Registers an empty <see cref="PolicyRegistry"/> in the service collection with service types
/// <see cref="IPolicyRegistry{String}"/>, and <see cref="IReadOnlyPolicyRegistry{String}"/> and returns /// <see cref="IPolicyRegistry{String}"/>, <see cref="IReadOnlyPolicyRegistry{String}"/>, and
/// the newly created registry. /// <see cref="IConcurrentPolicyRegistry{String}"/> and returns the newly created registry.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The newly created <see cref="IPolicyRegistry{String}"/>.</returns> /// <returns>The newly created <see cref="IPolicyRegistry{String}"/>.</returns>
...@@ -30,6 +30,8 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -30,6 +30,8 @@ namespace Microsoft.Extensions.DependencyInjection
// Create an empty registry, register and return it as an instance. This is the best way to get a // Create an empty registry, register and return it as an instance. This is the best way to get a
// single instance registered using both interfaces. // single instance registered using both interfaces.
var registry = new PolicyRegistry(); var registry = new PolicyRegistry();
services.AddSingleton<IConcurrentPolicyRegistry<string>>(registry);
services.AddSingleton<IPolicyRegistry<string>>(registry); services.AddSingleton<IPolicyRegistry<string>>(registry);
services.AddSingleton<IReadOnlyPolicyRegistry<string>>(registry); services.AddSingleton<IReadOnlyPolicyRegistry<string>>(registry);
...@@ -38,8 +40,8 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -38,8 +40,8 @@ namespace Microsoft.Extensions.DependencyInjection
/// <summary> /// <summary>
/// Registers the provided <see cref="IPolicyRegistry{String}"/> in the service collection with service types /// Registers the provided <see cref="IPolicyRegistry{String}"/> in the service collection with service types
/// <see cref="IPolicyRegistry{String}"/>, and <see cref="IReadOnlyPolicyRegistry{String}"/> and returns /// <see cref="IPolicyRegistry{String}"/>, <see cref="IReadOnlyPolicyRegistry{String}"/>, and
/// the provided registry. /// <see cref="IConcurrentPolicyRegistry{String}"/> and returns the provided registry.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="registry">The <see cref="IPolicyRegistry{String}"/>.</param> /// <param name="registry">The <see cref="IPolicyRegistry{String}"/>.</param>
...@@ -59,13 +61,18 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -59,13 +61,18 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<IPolicyRegistry<string>>(registry); services.AddSingleton<IPolicyRegistry<string>>(registry);
services.AddSingleton<IReadOnlyPolicyRegistry<string>>(registry); services.AddSingleton<IReadOnlyPolicyRegistry<string>>(registry);
if (registry is IConcurrentPolicyRegistry<string> concurrentRegistry)
{
services.AddSingleton<IConcurrentPolicyRegistry<string>>(concurrentRegistry);
}
return registry; return registry;
} }
/// <summary> /// <summary>
/// Registers an empty <see cref="PolicyRegistry"/> in the service collection with service types /// Registers an empty <see cref="PolicyRegistry"/> in the service collection with service types
/// <see cref="IPolicyRegistry{String}"/>, and <see cref="IReadOnlyPolicyRegistry{String}"/> and /// <see cref="IPolicyRegistry{String}"/>, <see cref="IReadOnlyPolicyRegistry{String}"/>, and
/// uses the specified delegate to configure it. /// <see cref="IConcurrentPolicyRegistry{String}"/> and uses the specified delegate to configure it.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configureRegistry">A delegate that is used to configure an <see cref="IPolicyRegistry{String}"/>.</param> /// <param name="configureRegistry">A delegate that is used to configure an <see cref="IPolicyRegistry{String}"/>.</param>
...@@ -93,6 +100,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -93,6 +100,7 @@ namespace Microsoft.Extensions.DependencyInjection
return registry; return registry;
}); });
services.AddSingleton<IConcurrentPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>());
services.AddSingleton<IPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>()); services.AddSingleton<IPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>());
services.AddSingleton<IReadOnlyPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>()); services.AddSingleton<IReadOnlyPolicyRegistry<string>>(serviceProvider => serviceProvider.GetRequiredService<PolicyRegistry>());
......
...@@ -11,6 +11,7 @@ using System.Threading.Tasks; ...@@ -11,6 +11,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Http; using Microsoft.Extensions.Http;
using Microsoft.Extensions.Http.Logging; using Microsoft.Extensions.Http.Logging;
using Polly; using Polly;
using Polly.Registry;
using Xunit; using Xunit;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
...@@ -468,6 +469,60 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -468,6 +469,60 @@ namespace Microsoft.Extensions.DependencyInjection
Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(HttpStatusCode.Created, response.StatusCode);
} }
[Fact]
public void AddPolicyHandlerFromRegistry_WithoutConfigureDelegate_AddsPolicyRegistries()
{
var serviceCollection = new ServiceCollection();
// Act
serviceCollection.AddPolicyRegistry();
var services = serviceCollection.BuildServiceProvider();
var registry = services.GetService<IPolicyRegistry<string>>();
// Assert
Assert.NotNull(registry);
Assert.Same(registry, services.GetService<IConcurrentPolicyRegistry<string>>());
Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
}
[Fact]
public void AddPolicyHandlerFromRegistry_WithRegistry_AddsPolicyRegistries()
{
var serviceCollection = new ServiceCollection();
var registry = new PolicyRegistry();
// Act
serviceCollection.AddPolicyRegistry(registry);
var services = serviceCollection.BuildServiceProvider();
// Assert
Assert.Same(registry, services.GetService<IConcurrentPolicyRegistry<string>>());
Assert.Same(registry, services.GetService<IPolicyRegistry<string>>());
Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
}
[Fact]
public void AddPolicyHandlerFromRegistry_WithConfigureDelegate_AddsPolicyRegistries()
{
var serviceCollection = new ServiceCollection();
// Act
serviceCollection.AddPolicyRegistry((serviceProvider, registry) =>
{
// No-op
});
var services = serviceCollection.BuildServiceProvider();
var registry = services.GetService<IPolicyRegistry<string>>();
// Assert
Assert.NotNull(registry);
Assert.Same(registry, services.GetService<IConcurrentPolicyRegistry<string>>());
Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
}
// Throws an exception or fails on even numbered requests, otherwise succeeds. // Throws an exception or fails on even numbered requests, otherwise succeeds.
private class FaultyMessageHandler : DelegatingHandler private class FaultyMessageHandler : DelegatingHandler
{ {
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册