@@ -30,87 +30,67 @@ public abstract class KeyValueConfigBuilder : ConfigurationBuilder
3030
3131 public override void Initialize ( string name , NameValueCollection config )
3232 {
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 ) ;
4234
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 ;
4740
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 ] ) ;
5244 }
5345
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+ }
5950 }
51+
52+ _cachedValues = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
6053 }
6154
6255 public override XmlNode ProcessRawXml ( XmlNode rawXml )
6356 {
64- try
65- {
66- if ( Mode == KeyValueMode . Expand )
67- return ExpandTokens ( rawXml ) ;
57+ if ( Mode == KeyValueMode . Expand )
58+ return ExpandTokens ( rawXml ) ;
6859
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 ;
7561 }
7662
7763 public override ConfigurationSection ProcessConfigurationSection ( ConfigurationSection configSection )
7864 {
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 )
8774 {
88- lock ( _cachedValues )
75+ if ( ! _greedyInited )
8976 {
90- if ( ! _greedyInited )
77+ foreach ( KeyValuePair < string , string > kvp in GetAllValuesInternal ( KeyPrefix ) )
9178 {
92- foreach ( KeyValuePair < string , string > kvp in GetAllValuesInternal ( KeyPrefix ) )
93- {
94- _cachedValues . Add ( kvp ) ;
95- }
96- _greedyInited = true ;
79+ _cachedValues . Add ( kvp ) ;
9780 }
81+ _greedyInited = true ;
9882 }
9983 }
84+ }
10085
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 ) ;
10988 }
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 ) ;
11391 }
92+
93+ return configSection ;
11494 }
11595
11696 private XmlNode ExpandTokens ( XmlNode rawXml )
@@ -244,7 +224,7 @@ private string GetValueInternal(string key)
244224 }
245225 catch ( Exception e )
246226 {
247- throw new ConfigurationErrorsException ( $ "Error in Configuration Builder '{ Name } '::GetValue({ key } )", e ) ;
227+ throw new Exception ( $ "Error in Configuration Builder '{ Name } '::GetValue({ key } )", e ) ;
248228 }
249229 }
250230
@@ -256,7 +236,7 @@ private ICollection<KeyValuePair<string, string>> GetAllValuesInternal(string pr
256236 }
257237 catch ( Exception e )
258238 {
259- throw new ConfigurationErrorsException ( $ "Error in Configuration Builder '{ Name } '::GetAllValues({ prefix } )", e ) ;
239+ throw new Exception ( $ "Error in Configuration Builder '{ Name } '::GetAllValues({ prefix } )", e ) ;
260240 }
261241 }
262242 }
0 commit comments