@@ -30,87 +30,67 @@ public abstract class KeyValueConfigBuilder : ConfigurationBuilder
30
30
31
31
public override void Initialize ( string name , NameValueCollection config )
32
32
{
33
- try
34
- {
35
- base . Initialize ( name , config ) ;
36
-
37
- // Override default config
38
- if ( config != null )
39
- {
40
- KeyPrefix = config [ prefixTag ] ?? "" ;
41
- TokenPattern = config [ tokenPatternTag ] ?? TokenPattern ;
33
+ base . Initialize ( name , config ) ;
42
34
43
- if ( config [ stripPrefixTag ] != null ) {
44
- // We want an exception here if 'stripPrefix' is specified but unrecognized.
45
- _stripPrefix = Boolean . Parse ( config [ stripPrefixTag ] ) ;
46
- }
35
+ // Override default config
36
+ if ( config != null )
37
+ {
38
+ KeyPrefix = config [ prefixTag ] ?? "" ;
39
+ TokenPattern = config [ tokenPatternTag ] ?? TokenPattern ;
47
40
48
- if ( config [ modeTag ] != null ) {
49
- // We want an exception here if 'mode' is specified but unrecognized.
50
- Mode = ( KeyValueMode ) Enum . Parse ( typeof ( KeyValueMode ) , config [ modeTag ] , true ) ;
51
- }
41
+ if ( config [ stripPrefixTag ] != null ) {
42
+ // We want an exception here if 'stripPrefix' is specified but unrecognized.
43
+ _stripPrefix = Boolean . Parse ( config [ stripPrefixTag ] ) ;
52
44
}
53
45
54
- _cachedValues = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
55
- }
56
- catch ( Exception e )
57
- {
58
- throw new ConfigurationErrorsException ( $ "Error while initializing Configuration Builder '{ name } '. ({ e . Message } )", e ) ;
46
+ if ( config [ modeTag ] != null ) {
47
+ // We want an exception here if 'mode' is specified but unrecognized.
48
+ Mode = ( KeyValueMode ) Enum . Parse ( typeof ( KeyValueMode ) , config [ modeTag ] , true ) ;
49
+ }
59
50
}
51
+
52
+ _cachedValues = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
60
53
}
61
54
62
55
public override XmlNode ProcessRawXml ( XmlNode rawXml )
63
56
{
64
- try
65
- {
66
- if ( Mode == KeyValueMode . Expand )
67
- return ExpandTokens ( rawXml ) ;
57
+ if ( Mode == KeyValueMode . Expand )
58
+ return ExpandTokens ( rawXml ) ;
68
59
69
- return rawXml ;
70
- }
71
- catch ( Exception e )
72
- {
73
- throw new ConfigurationErrorsException ( $ "Error while processing xml in Configuration Builder '{ Name } '. ({ e . Message } )", e ) ;
74
- }
60
+ return rawXml ;
75
61
}
76
62
77
63
public override ConfigurationSection ProcessConfigurationSection ( ConfigurationSection configSection )
78
64
{
79
- try {
80
- // Expand mode works on the raw string input
81
- if ( Mode == KeyValueMode . Expand )
82
- return configSection ;
83
-
84
- // In Greedy mode, we need to know all the key/value pairs from this config source. So we
85
- // can't 'cache' them as we go along. Slurp them all up now. But only once. ;)
86
- if ( ( Mode == KeyValueMode . Greedy ) && ( ! _greedyInited ) )
65
+ // Expand mode works on the raw string input
66
+ if ( Mode == KeyValueMode . Expand )
67
+ return configSection ;
68
+
69
+ // In Greedy mode, we need to know all the key/value pairs from this config source. So we
70
+ // can't 'cache' them as we go along. Slurp them all up now. But only once. ;)
71
+ if ( ( Mode == KeyValueMode . Greedy ) && ( ! _greedyInited ) )
72
+ {
73
+ lock ( _cachedValues )
87
74
{
88
- lock ( _cachedValues )
75
+ if ( ! _greedyInited )
89
76
{
90
- if ( ! _greedyInited )
77
+ foreach ( KeyValuePair < string , string > kvp in GetAllValuesInternal ( KeyPrefix ) )
91
78
{
92
- foreach ( KeyValuePair < string , string > kvp in GetAllValuesInternal ( KeyPrefix ) )
93
- {
94
- _cachedValues . Add ( kvp ) ;
95
- }
96
- _greedyInited = true ;
79
+ _cachedValues . Add ( kvp ) ;
97
80
}
81
+ _greedyInited = true ;
98
82
}
99
83
}
84
+ }
100
85
101
- if ( configSection is AppSettingsSection ) {
102
- return ProcessAppSettings ( ( AppSettingsSection ) configSection ) ;
103
- }
104
- else if ( configSection is ConnectionStringsSection ) {
105
- return ProcessConnectionStrings ( ( ConnectionStringsSection ) configSection ) ;
106
- }
107
-
108
- return configSection ;
86
+ if ( configSection is AppSettingsSection ) {
87
+ return ProcessAppSettings ( ( AppSettingsSection ) configSection ) ;
109
88
}
110
- catch ( Exception e )
111
- {
112
- throw new ConfigurationErrorsException ( $ "Error while processing configSection in Configuration Builder '{ Name } '. ({ e . Message } )", e ) ;
89
+ else if ( configSection is ConnectionStringsSection ) {
90
+ return ProcessConnectionStrings ( ( ConnectionStringsSection ) configSection ) ;
113
91
}
92
+
93
+ return configSection ;
114
94
}
115
95
116
96
private XmlNode ExpandTokens ( XmlNode rawXml )
@@ -244,7 +224,7 @@ private string GetValueInternal(string key)
244
224
}
245
225
catch ( Exception e )
246
226
{
247
- throw new ConfigurationErrorsException ( $ "Error in Configuration Builder '{ Name } '::GetValue({ key } )", e ) ;
227
+ throw new Exception ( $ "Error in Configuration Builder '{ Name } '::GetValue({ key } )", e ) ;
248
228
}
249
229
}
250
230
@@ -256,7 +236,7 @@ private ICollection<KeyValuePair<string, string>> GetAllValuesInternal(string pr
256
236
}
257
237
catch ( Exception e )
258
238
{
259
- throw new ConfigurationErrorsException ( $ "Error in Configuration Builder '{ Name } '::GetAllValues({ prefix } )", e ) ;
239
+ throw new Exception ( $ "Error in Configuration Builder '{ Name } '::GetAllValues({ prefix } )", e ) ;
260
240
}
261
241
}
262
242
}
0 commit comments