-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServiceCollectionExtensions.cs
39 lines (34 loc) · 1.64 KB
/
ServiceCollectionExtensions.cs
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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace Duber.Infrastructure.Chaos.IoC
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddChaosApiHttpClient(this IServiceCollection services, IConfiguration configuration)
{
services.AddHttpClient<ChaosApiHttpClient>(client =>
{
client.Timeout = TimeSpan.FromSeconds(5);
client.BaseAddress = new Uri(configuration.GetValue<string>("ChaosApiSettings:BaseUrl"));
});
if (configuration.GetValue<bool>("UseAzureAppConfiguration"))
services.Configure<GeneralChaosSetting>(configuration.GetSection("GeneralChaosSetting"));
services.AddScoped<Lazy<Task<GeneralChaosSetting>>>(sp =>
{
if (configuration.GetValue<bool>("UseAzureAppConfiguration"))
{
var chaosSettings = sp.GetRequiredService<IOptionsSnapshot<GeneralChaosSetting>>();
return new Lazy<Task<GeneralChaosSetting>>(() => Task.FromResult(chaosSettings.Value), LazyThreadSafetyMode.None);
}
// we use LazyThreadSafetyMode.None in order to avoid locking.
var chaosApiHttpClient = sp.GetRequiredService<ChaosApiHttpClient>();
return new Lazy<Task<GeneralChaosSetting>>(() => chaosApiHttpClient.GetGeneralChaosSettings(), LazyThreadSafetyMode.None);
});
return services;
}
}
}