1

We recently took over the maintenance a WCF Web service. While looking at the code, I saw that the service needed to access around 10 settings (string key-value pairs) very often and each time it needed this it used to load the settings.xml file into a XmlDocument and then access the value.

I believe that this is not ideal and have tried out the following approaches.

  1. De-serialization:

    • Created a settings class with all the settings I needed.
    • Used a small tool to serialize the class into a file, which I copied to my service folder.
    • I have De-serialized the saved settings as JSON (Using ServiceStack JSON library), as XML (using .NET XmlSerializer) and as binary(using BinaryFormatter).
  2. appSettings in Web.Config:

    • I also tried saving the settings in the web.Config file and was able to retrieve them.

Both approaches worked.

I would like to know which of the above 2 alternatives that I have tried is the best to save settings for my webservice? If there is some other alternative please feel free to point it out.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • Why not store them in the config file, and then load them into cache? Option 1 seems like an awful lot of work, unless you have no other choice. – Tim Apr 16 '13 at 06:45
  • @Tim: Hi, Can you please refer to my comment to Sergey's answer below? – Patrick D'Souza Apr 16 '13 at 06:56

1 Answers1

2

Generally, custom app settings are saved in web.config. This has two advantages: easy to read using built-in functions and offers automatic app reload when settings are updated.

Sergey
  • 2,303
  • 1
  • 18
  • 11
  • Are you referring to loading settings as mentioned in this [SO Question](http://stackoverflow.com/q/8147220/1012641)? I have tried this out. Its just that I wanted to know which is the best approach. Can you provide me any links to confirm that approach 2 in the question is better? – Patrick D'Souza Apr 16 '13 at 06:50
  • 1
    @Romulus - I don't know of any links or such to confirm which approach is better, but I think common sense would indicate that using the framework's built-in functionality is always better than rolling your own solution, unless the framework simply will not support what you're trying to do. It's also a lot easier for developers who come along behind you to comprehend what you're doing, rather than looking for documentation (if it exists) or having to infer from the code why you made a custom settings class when 99.9% of the .NET world would use the config file. :) – Tim Apr 16 '13 at 06:59
  • Automatic app reload on config file changes, did not know that. +1 – Patrick D'Souza Apr 16 '13 at 07:10
  • Can types (int or bool) be defined in the appSettings or will I have to handle the typecasting if I need int or bool's later? – Patrick D'Souza Apr 16 '13 at 07:19
  • @Romulus - you will need to handle the casts yourself. – Tim Apr 16 '13 at 21:57
  • @Tim: Thanks to both of you. I have started implementation as you both suggested – Patrick D'Souza Apr 17 '13 at 02:29