Originally posted by: Apathetic
Originally posted by: Dhaval00
Originally posted by: Apathetic
On service startup, read all the values in your app.config file and VALIDATE THEM. Put the validated values in a hashtable (or something similar). The worker thread then reads all the (now validated) values it needs from the hashtable.
If you run into an invalid value in your app.config file, the service should a ConfigurationException and refuse to start. Remember ALL input is EVIL and should be treated as such.
Dave
Why the extra work? AppSettings get loaded in-memory any way. If this is a .NET 2.0 or greater application, just use type-safe settings. The Framework does all the work for you and you can reference them in a type-safe manner using Properties.Settings.Default (assuming all the settings are Application-scoped).
The AppSettings stuff is very simplistic. Let's say I have an app.config entry named "ImageDirectory" which must be set and contain a valid path in order for the service to run. About all I can do in AppSettings is say there's an entry called ImageDirectory and it's a string. My way, I validate that the entry exists, is a valid UNC path, and the service has access to it at runtime. Also, when I add the ImageDirectory to the hashtable, I can make sure it ends with a backslash ("\") character. Now when I need to refer to the directory in code to build a path, I can remove all the checks for a trailing backslash because I know it's always there.
Another example: a "NumberOfWorkerThreads" entry in app.config. I can say that not only does this entry have to be an integer, but an integer between 1 and 10.
I do a bunch of validation, but I do it all up front and then never worry about it in the rest of my code.
Dave