-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
49 lines (41 loc) · 2 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
49 lines (41 loc) · 2 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
using System.Configuration;
using System.Linq;
using System.Text;
using CacheIt.Hosting;
using CacheIt.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace CacheIt.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
private static void ValidateCustomRefreshOptions(IConfiguration configuration)
{
var customRefreshOptionsSection = configuration.GetSection(CustomRefreshOptions.SectionName);
var refreshTimesByCacheableName = customRefreshOptionsSection.GetSection("RefreshTimesByCacheableName").GetChildren();
var misconfiguredChildren = refreshTimesByCacheableName.Where(child => int.TryParse(child.Value, out _));
var areThereAnyMisconfiguredChildren = misconfiguredChildren.Any();
if (areThereAnyMisconfiguredChildren)
{
var builder = new StringBuilder();
foreach(var child in misconfiguredChildren)
builder.AppendLine($"-> {child.Path}");
throw new ConfigurationErrorsException($"There are Cacheables configured with type 'int', this version is defined for timestamp configurations!"
+ $"{System.Environment.NewLine} Please review Your configurations for the following:{System.Environment.NewLine}"
+ $"{builder.ToString()}");
}
}
public static IServiceCollection AddCacheIt(this IServiceCollection services)
{
services.AddHostedService<Handler>();
return services;
}
public static IServiceCollection AddCacheIt(this IServiceCollection services, IConfiguration configuration)
{
ValidateCustomRefreshOptions(configuration);
services.AddHostedService<Handler>();
services.Configure<CustomRefreshOptions>(configuration.GetSection(CustomRefreshOptions.SectionName));
return services;
}
}
}