CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4517

C# web.config List of services

I want to store a list of services (servers and service names) in a web.config file.
There could be any number and I need to be able to sort them in stop/start order.

Of course I can do this in any number of ways, but this is the kind of thing I need to do time and time again and I'm looking for the most elegant solutions.

So what are the most elegant solutions for
(a) storing a number of multi-attribute elements (serviceName, serverName, order) in web.config, and
(b) sorting them into their specified order

Thanks for all suggestions :)

Upvotes: 0

Views: 679

Answers (3)

Despertar
Despertar

Reputation: 22372

Normally in a .config you just have key/value pairs in your appSettings. However if you have more than two items you can choose a delimiter to split additional items in the value string. Each item is just based on position and you can have as many comma delimited values as you want. You can use any value for the key and you can always sort within the LINQ.

       <appSettings>
          <add key="order" value="servername, ipaddress, anothervalue"/>
       </appSettings>

       var cfg = ConfigurationManager.AppSettings;

        var servers =
            from key in cfg.AllKeys
            select new {
                order = key,
                servername = cfg[key].Split(',')[0],
                ipaddress = cfg[key].Split(',')[1],
                anothervalue = cfg[key].Split(',')[2]
            };

Upvotes: -1

Lloyd
Lloyd

Reputation: 2942

Create a Custom ConfigurationSection, this allows you to define your own Configuration Secion, Elements and Sorting on the collection, how to manage duplicate entries etc.

Something alone these lines:

<section name="myServices" type="MyNameSpace.MyCustomConfigurationSectionHandler, MyAssembly, Version=1.0.0.0, Culture=neutral" restartOnExternalChanges="false" requirePermission="false" />

// I've placed order into the setting in case it is to be persisted or predefined
<myServices>    
  <add serviceName="MyFirstService" serverName="MyService" order="0" />
</myServices>

Upvotes: 1

chandmk
chandmk

Reputation: 3481

Keep then in xml file and use linq to xml to query, sort them to your needs

Upvotes: 1

Related Questions