Skip to content

Commit 134e1a0

Browse files
committed
AddPollyTest
1 parent 23d07de commit 134e1a0

5 files changed

Lines changed: 79 additions & 15 deletions

File tree

src/plugins/ConsolePollyTest/PollyTest.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Polly;
2+
using Polly.Retry;
23

34
namespace ConsolePollyTest;
45

@@ -7,9 +8,63 @@ public static class PollyTest
78

89
#region Constants & Statics
910

10-
public static void MethodName()
11+
public static Uri Host { get; set; } = new Uri("http://localhost:5222");
12+
13+
private static HttpClient GetClient()
14+
{
15+
var client = new HttpClient { BaseAddress = Host };
16+
return client;
17+
}
18+
19+
private static Uri GetUri(string path)
20+
{
21+
return new Uri(Host, path);
22+
}
23+
24+
public static async Task NoStrategy_TestAsync()
25+
{
26+
var builder = new ResiliencePipelineBuilder();
27+
var pipeline = builder.Build();
28+
29+
using var client = GetClient();
30+
31+
var result = await pipeline.ExecuteAsync(
32+
async (token) =>
33+
{
34+
var r = await client.GetAsync(GetUri("/weatherforecast/"), token);
35+
return r;
36+
},
37+
cancellationToken: default);
38+
39+
Console.WriteLine(result);
40+
}
41+
42+
public static async Task Retry_ExecThree_TestAsync()
1143
{
12-
var builder = new ResiliencePipelineBuilder().;
44+
var builder = new ResiliencePipelineBuilder().AddRetry(
45+
new RetryStrategyOptions
46+
{
47+
Delay = TimeSpan.FromSeconds(1),
48+
MaxRetryAttempts = 2,
49+
ShouldHandle = (args) =>
50+
{
51+
Console.WriteLine(args.AttemptNumber);
52+
return ValueTask.FromResult(args.AttemptNumber >= 0);
53+
}
54+
});
55+
var pipeline = builder.Build();
56+
57+
using var client = GetClient();
58+
59+
var result = await pipeline.ExecuteAsync(
60+
async (token) =>
61+
{
62+
var r = await client.GetAsync(GetUri("/weatherforecast/"), token);
63+
return r;
64+
},
65+
cancellationToken: default);
66+
67+
Console.WriteLine(result);
1368
}
1469

1570
#endregion

src/plugins/ConsolePollyTest/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ internal static class Program
55

66
#region Constants & Statics
77

8-
private static void Main(string[] args)
8+
private static async Task Main(string[] args)
99
{
10+
//await PollyTest.NoStrategy_TestAsync();
11+
await PollyTest.Retry_ExecThree_TestAsync();
12+
13+
_ = Console.ReadKey();
1014
}
1115

1216
#endregion

src/plugins/WebPollyTest/Program.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.AspNetCore.HttpLogging;
2+
13
namespace WebPollyTest;
24

35
internal static class Program
@@ -9,21 +11,29 @@ private static void Main(string[] args)
911
{
1012
var builder = WebApplication.CreateBuilder(args);
1113

12-
// Add services to the container.
14+
_ = builder.Logging.AddConsole();
1315

1416
_ = builder.Services.AddControllers();
15-
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
1617
_ = builder.Services.AddOpenApi();
1718

19+
_ = builder.Services
20+
.AddHttpLogging(
21+
(options) =>
22+
{
23+
options.LoggingFields = HttpLoggingFields.All;
24+
options.CombineLogs = true;
25+
});
26+
1827
var app = builder.Build();
1928

20-
// Configure the HTTP request pipeline.
29+
_ = app.UseHttpLogging();
30+
2131
if (app.Environment.IsDevelopment())
2232
{
2333
_ = app.MapOpenApi();
2434
}
2535

26-
_ = app.UseHttpsRedirection();
36+
//_ = app.UseHttpsRedirection();
2737

2838
_ = app.UseAuthorization();
2939

@@ -34,4 +44,4 @@ private static void Main(string[] args)
3444

3545
#endregion
3646

37-
}
47+
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
6-
}
7-
}
82
}

src/plugins/WebPollyTest/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Logging": {
33
"LogLevel": {
44
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
5+
"Microsoft.AspNetCore": "Warning",
6+
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
67
}
78
},
89
"AllowedHosts": "*"

0 commit comments

Comments
 (0)