forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCoreAppSettings.cs
More file actions
107 lines (90 loc) · 3.28 KB
/
Copy pathNetCoreAppSettings.cs
File metadata and controls
107 lines (90 loc) · 3.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#if NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Microsoft.Extensions.Configuration;
using ServiceStack.Configuration;
using ServiceStack.Text;
namespace ServiceStack
{
public class NetCoreAppSettings : IAppSettings
{
public IConfiguration Configuration { get; }
public NetCoreAppSettings(IConfiguration configuration) => Configuration = configuration;
private static T Bind<T>(IConfigurationSection config)
{
try
{
if (config.Value != null)
return config.Value.ConvertTo<T>();
}
catch (Exception ex)
{
var message = $"The {config.Key} setting had an invalid format. " +
$"The value \"{config.Value}\" could not be cast to type {typeof(T).FullName}";
throw new ConfigurationErrorsException(message, ex);
}
try
{
var to = typeof(T).CreateInstance();
config.Bind(to);
return (T)to;
}
catch (InvalidOperationException ex)
{
throw new ConfigurationErrorsException(ex.Message, ex);
}
}
private IConfigurationSection GetRequiredSection(string key)
{
var child = Configuration.GetChildren().FirstOrDefault(x => x.Key == key);
if (child == null)
throw new ConfigurationErrorsException(string.Format(ErrorMessages.AppsettingNotFound, key));
return child;
}
public Dictionary<string, string> GetAll()
{
var to = new Dictionary<string, string>();
foreach (var child in Configuration.GetChildren())
{
if (child.Value == null)
continue;
var key = child.Key;
to[key] = child.Value;
}
return to;
}
public List<string> GetAllKeys() => Configuration.GetChildren().Select(child => child.Key).ToList();
public bool Exists(string key) => Configuration.GetChildren().Any(x => x.Key == key);
public void Set<T>(string key, T value) => Configuration[key] = value is string
? value.ToString()
: TypeSerializer.SerializeToString(value);
public string GetString(string name) => Configuration[name];
public IList<string> GetList(string key)
{
var section = GetRequiredSection(key);
var members = section.GetChildren();
var to = members.Map(x => x.Value);
return to;
}
public IDictionary<string, string> GetDictionary(string key)
{
var to = Bind<Dictionary<string,string>>(GetRequiredSection(key));
return to;
}
public T Get<T>(string name)
{
return Bind<T>(Configuration.GetSection(name));
}
public T Get<T>(string name, T defaultValue)
{
var child = Configuration.GetChildren().FirstOrDefault(x => x.Key == name);
if (child?.Value == null)
return defaultValue;
var to = Bind<T>(child);
return to;
}
}
}
#endif