Browse Source

Tidy up how custom configuration sections work

Antony Male 9 years ago
parent
commit
b5b0f9c88b
2 changed files with 15 additions and 7 deletions
  1. 1 1
      src/SyncTrayzor/App.config
  2. 14 6
      src/SyncTrayzor/Services/Config/AppSettings.cs

+ 1 - 1
src/SyncTrayzor/App.config

@@ -2,7 +2,7 @@
 <configuration>
     <configSections>
       <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
-      <section name="settings" type="SyncTrayzor.Services.Config.AppSettingsConfigurationHandler, SyncTrayzor" />
+      <section name="settings" type="SyncTrayzor.Services.Config.XmlConfigurationSection, SyncTrayzor" />
     </configSections>
   
     <startup> 

+ 14 - 6
src/SyncTrayzor/Services/Config/AppSettings.cs

@@ -5,25 +5,33 @@ using System.Xml.Serialization;
 
 namespace SyncTrayzor.Services.Config
 {
-    public class AppSettingsConfigurationHandler : ConfigurationSection
+    public class XmlConfigurationSection : ConfigurationSection
     {
-        private object appSettings;
+        private XmlReader reader;
 
         protected override void DeserializeSection(XmlReader reader)
         {
-            var serializer = new XmlSerializer(typeof(AppSettings), new XmlRootAttribute(this.SectionInformation.Name));
-            this.appSettings = serializer.Deserialize(reader);
+            this.reader = reader;
         }
 
         protected override object GetRuntimeObject()
         {
-            return this.appSettings;
+            return this.reader;
         }
     }
 
     public class AppSettings
     {
-        public static readonly AppSettings Instance = (AppSettings)ConfigurationManager.GetSection("settings");
+        private const string sectionName = "settings";
+
+        public static readonly AppSettings Instance;
+
+        static AppSettings()
+        {
+            var reader = (XmlReader)ConfigurationManager.GetSection(sectionName);
+            var serializer = new XmlSerializer(typeof(AppSettings), new XmlRootAttribute(sectionName));
+            Instance = (AppSettings)serializer.Deserialize(reader);
+        }
 
         public string UpdateApiUrl { get; set; } = "http://synctrayzor.antonymale.co.uk/version-check";
         public string HomepageUrl { get; set; } = "http://github.com/canton7/SyncTrayzor";