11namespace ServiceControl . Configuration
22{
3+ using System ;
34 using System . Configuration ;
45 using System . IO ;
56 using System . Linq ;
@@ -12,27 +13,37 @@ public static class ExeConfiguration
1213 // This code reads in the exe.config files and adds all the values into the ConfigurationManager's collections.
1314 public static void PopulateAppSettings ( Assembly assembly )
1415 {
15- var location = Path . GetDirectoryName ( assembly . Location ) ;
16- var assemblyName = Path . GetFileNameWithoutExtension ( assembly . Location ) ;
17- var exeConfigPath = Path . Combine ( location , $ "{ assemblyName } .exe.config") ;
18- var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = exeConfigPath } ;
19- var configuration = ConfigurationManager . OpenMappedExeConfiguration ( fileMap , ConfigurationUserLevel . None ) ;
20-
21- foreach ( var key in configuration . AppSettings . Settings . AllKeys )
16+ try
2217 {
23- ConfigurationManager . AppSettings . Set ( key , configuration . AppSettings . Settings [ key ] . Value ) ;
18+ var location = Path . GetDirectoryName ( assembly . Location ) ;
19+ var assemblyName = Path . GetFileNameWithoutExtension ( assembly . Location ) ;
20+ var exeConfigPath = Path . Combine ( location , $ "{ assemblyName } .exe.config") ;
21+ var fileMap = new ExeConfigurationFileMap
22+ {
23+ ExeConfigFilename = exeConfigPath
24+ } ;
25+ var configuration = ConfigurationManager . OpenMappedExeConfiguration ( fileMap , ConfigurationUserLevel . None ) ;
26+
27+ foreach ( var key in configuration . AppSettings . Settings . AllKeys )
28+ {
29+ ConfigurationManager . AppSettings . Set ( key , configuration . AppSettings . Settings [ key ] . Value ) ;
30+ }
31+
32+ // The connection strings collection has had its read only flag set, so we need to clear it before we can add items to it
33+ UnsetCollectionReadonly ( ConfigurationManager . ConnectionStrings ) ;
34+
35+ foreach ( var connectionStringSetting in configuration . ConnectionStrings . ConnectionStrings . Cast < ConnectionStringSettings > ( ) )
36+ {
37+ ConfigurationManager . ConnectionStrings . Add ( connectionStringSetting ) ;
38+ }
39+
40+ // Put the collection back into its previous state after we're done adding items to it
41+ SetCollectionReadOnly ( ConfigurationManager . ConnectionStrings ) ;
2442 }
25-
26- // The connection strings collection has had its read only flag set, so we need to clear it before we can add items to it
27- UnsetCollectionReadonly ( ConfigurationManager . ConnectionStrings ) ;
28-
29- foreach ( var connectionStringSetting in configuration . ConnectionStrings . ConnectionStrings . Cast < ConnectionStringSettings > ( ) )
43+ catch ( Exception e )
3044 {
31- ConfigurationManager . ConnectionStrings . Add ( connectionStringSetting ) ;
45+ throw new Exception ( "Failed to populate app settings." , e ) ;
3246 }
33-
34- // Put the collection back into its previous state after we're done adding items to it
35- SetCollectionReadOnly ( ConfigurationManager . ConnectionStrings ) ;
3647 }
3748
3849 static void UnsetCollectionReadonly ( ConfigurationElementCollection collection )
@@ -47,4 +58,4 @@ static void UnsetCollectionReadonly(ConfigurationElementCollection collection)
4758 [ UnsafeAccessor ( UnsafeAccessorKind . Method , Name = "SetReadOnly" ) ]
4859 static extern void SetCollectionReadOnly ( ConfigurationElementCollection collection ) ;
4960 }
50- }
61+ }
0 commit comments