1/// <summary>
2 /// Section Group which contains all the configuration
3 /// </summary>
4 public class HeartBeatConfigurationSectionGroup : ConfigurationSectionGroup
5 {
6 #region Constants ...
7
8
9 public const string GROUP_NAME = "Services.HeartBeat";
10 #endregion
11 #region Private Helper Methods ...
12
13 #endregion
14 #region Public Properties ...
15
16
17 /// <summary>
18 /// This is the public property which returns the current configuration
19 /// file mapped to the running system.
20 /// </summary>
21 public static HeartBeatConfigurationSectionGroup ConfigSection
22 {
23 get
24 {
25 var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
26 return (HeartBeatConfigurationSectionGroup)config.GetSectionGroup(GROUP_NAME);
27 }
28 }
29 #endregion
30 }
31
32//====================================================
33
34public class ServicesConfigurationSection : ConfigurationSection
35 {
36 #region Constants ...
37
38 public static readonly string SECTION_NAME = HeartBeatConfigurationSectionGroup.GROUP_NAME + @"/Services";
39 public const string SUB_SECTION_NAME = "Service";
40 #endregion
41 #region Public Properties ...
42
43
44 /// <summary>
45 /// Represents the ListOfServices. This has been private
46 /// as instead of this Services property would be used.
47 /// </summary>
48 [ConfigurationProperty(SUB_SECTION_NAME)]
49 private AddCollection ListOfServices
50 {
51 get { return (AddCollection)this[SUB_SECTION_NAME]; }
52 }
53
54 /// <summary>
55 /// This property represents the services which needs to be monitored.
56 /// </summary>
57 public List<AddElement> Services
58 {
59 get { return ListOfServices.Cast<AddElement>().ToList(); }
60 }
61
62 /// <summary>
63 /// Represents the current email section
64 /// </summary>
65 public static ServicesConfigurationSection ConfigSection
66 {
67 get
68 {
69 var errorMsg = Validate();
70 if (!string.IsNullOrEmpty(errorMsg))
71 throw new Exception(errorMsg);
72 return (ServicesConfigurationSection)ConfigurationManager.GetSection(SECTION_NAME);
73 }
74 }
75
76 /// <summary>
77 /// This method validates if all the required key(s) have been provided
78 /// or not.
79 /// </summary>
80 /// <returns></returns>
81 private static string Validate()
82 {
83 //try to get the services section
84 var configurationObject = ConfigurationManager.GetSection(SECTION_NAME);
85
86 //If there is no section return it from there itself.
87 if (configurationObject == null)
88 return string.Format("section missing", SECTION_NAME);
89
90 //Type cast in to the strongly type object
91 var stronglyTypedObject = (ServicesConfigurationSection)configurationObject;
92
93 //Get the service section
94 var serviceSection = stronglyTypedObject[SUB_SECTION_NAME];
95
96 //If no Service element, return the error message
97 if (serviceSection == null)
98 return string.Format("sub section missing", SUB_SECTION_NAME, SECTION_NAME);
99
100 //Type cast in strongly types object
101 var stronglyTypedServiceCollectionObject = (AddCollection)serviceSection;
102
103 //If no service name has been found, return the error message
104 if (stronglyTypedServiceCollectionObject == null || stronglyTypedServiceCollectionObject.Count == 0)
105 return string.Format("require atleast one name", SUB_SECTION_NAME);
106
107 return string.Empty;
108 }
109 #endregion
110 }
111
112
113//====================================================
114
115[ConfigurationCollection(typeof(AddElement))]
116 public class AddCollection : ConfigurationElementCollection
117 {
118 /// <summary>
119 /// Override the method
120 /// </summary>
121 /// <returns></returns>
122 protected override ConfigurationElement CreateNewElement()
123 {
124 return new AddElement();
125 }
126
127 /// <summary>
128 /// Override the GetElementKey
129 /// </summary>
130 /// <param name="element"></param>
131 /// <returns></returns>
132 protected override object GetElementKey(ConfigurationElement element)
133 {
134 return ((AddElement)(element)).Name;
135 }
136
137 /// <summary>
138 /// Create the indexer
139 /// </summary>
140 /// <param name="idx"></param>
141 /// <returns></returns>
142 public AddElement this[int idx]
143 {
144 get
145 {
146 return (AddElement)BaseGet(idx);
147 }
148 }
149 }
150
151//====================================================
152
153 public class AddElement : ConfigurationElement
154 {
155 /// <summary>
156 /// Represents the name of the key.
157 /// </summary>
158 [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = false)]
159 public string Name
160 {
161 get { return ((string)(base["name"])); }
162
163 }
164
165 }