2

ASP.NET MVC4 C# application web.config contains setting like

<configuration>
  <appSettings>
    <add key="DefaultDataBase"
         value="db1"/>
  ..

Method

public static string DefaultDataBase
{
    get
    {
        return WebConfigurationManager.AppSettings["DefaultDataBase"] ?? "mydefault";
    }
}

Is used to get it in code. If web.config accidently contains duplicate key like

<configuration>
  <appSettings>
    <add key="DefaultDataBase"
         value="db1"/>
    <add key="DefaultDataBase"
         value="db2"/>

  ..

this code silently resturns wrong value db2

How to detect duplicate keys and throw ApplicationException on duplicates ?

Andrus
  • 26,339
  • 60
  • 204
  • 378
  • AppSettings has Keys property....you can iterate over it and check for key "DefaultDatabase" and if count is more than one then you have duplicate keys...I am not sure If linq query is supported on this type...but if it is then linq query would be even better – Viru Feb 12 '16 at 13:59
  • _"How to detect"_ - XY problem. Even if you could detect it, it won't solve the problem, as your application doesn't know which one to use. Fix the original problem: use proper deployment techniques, don't manually edit configuration files. – CodeCaster Feb 12 '16 at 14:07
  • 1
    Real web.config file appsettings part is large. It has large number of comments and lot of settings. It is easy to add some duplicate setting. I asked how to thorow error if there is duplicate setting in web.config. Manula editing is convenient and fast way, no need to change this. – Andrus Feb 12 '16 at 20:02

1 Answers1

0

It is not possible using AppSettings property. It is just a normal dictionary, when you set data in the key that have already some data then this data is replaced.

You can check duplicates by reading web.config xml using for example XmlReader (or other techniques). But this is not common mechanism as config file is something that should be changed by "responsible" people.

  • xml manual parsings seems too much work for this little task. Some part of .NET reads web.config file. Maybe it is possible to set some property which prevents .NET to overwrite property and throw exception instead? Or override some web.config reader method ? – Andrus Feb 12 '16 at 20:05
  • 1
    It is not possible to override existing methods. But I came up with new idea, you can create custom configuration section like here: https://msdn.microsoft.com/pl-pl/library/2tw134k3.aspx?f=255&MSPPError=-2147217396 and then I think you can detect in your class that there are duplicated entries. – Marcin Iwanowski Feb 13 '16 at 09:48
  • Thank you. This is great idea. In this case it is possible hopefully to restrict key names to predefined list of names also. Where to find sample code which prevents duplicates in custom section? Should code in setter throw exception if key already exists? – Andrus Feb 13 '16 at 18:51
  • I looked into it a little bit deeper and idea with custom configuration section is a little harder. You have to define GetElementKey function that return unique key for an element, in your case it should return combination of "key" and "value". Check this: http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection After modifying GetElementKey function you can implement in ServiceCollection method that will check and throw exception if there are duplicated entries and call it for example in Services property (or in indexer property) – Marcin Iwanowski Feb 15 '16 at 08:45
  • I think that manually reading xml and checking for duplicates is much simpler :) – Marcin Iwanowski Feb 15 '16 at 08:49