-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (42 loc) · 1.68 KB
/
Program.cs
File metadata and controls
50 lines (42 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
using Polly.Registry;
using Polly.Retry;
using Vultar;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddOptions<VultarSettings>()
.BindConfiguration(VultarSettings.SectionName)
.ValidateDataAnnotations()
.ValidateOnStart();
builder.Services.AddResiliencePipeline("default", (pipeline, context) =>
{
var options = context.ServiceProvider.GetRequiredService<IOptions<VultarSettings>>().Value;
var logger = context.ServiceProvider.GetRequiredService<ILogger<Worker>>();
pipeline.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = options.MaxRetryAttempts,
Delay = options.RetryDelay,
BackoffType = DelayBackoffType.Exponential,
ShouldHandle = new PredicateBuilder().Handle<Exception>(),
OnRetry = args =>
{
logger.Retrying(args.AttemptNumber, args.RetryDelay);
return ValueTask.CompletedTask;
}
});
});
builder.Services.AddSingleton(sp =>
sp.GetRequiredService<ResiliencePipelineProvider<string>>().GetPipeline("default"));
builder.Services.Configure<HostOptions>(options =>
{
var settings = builder.Configuration.GetSection(VultarSettings.SectionName).Get<VultarSettings>();
options.ShutdownTimeout = settings?.ShutdownTimeout ?? TimeSpan.FromSeconds(30);
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.StopHost;
});
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
await host.RunAsync();