5

I have the following section in my Web.config file:

<configSections>
    <section name="mySection" type="myNameSpace, myProject"/>
</configSections>


<mySection>
    <city id="ny" type="nameSpace1" />
    <city id="dc" type="nameSpace2" />
    <city id="nj" type="nameSpace3" />
</mySection>

I need to write code that loops through the cities given the id and return the type.

i.e.

 if the given id = "ny" --> return nameSpace1
 if the given id = "dc" --> return nameSpace2
 if the given id = "nj" --> return nameSpace3
default
  • 11,485
  • 9
  • 66
  • 102
bombo
  • 1,781
  • 6
  • 22
  • 25
  • Well, I assume you created a type for the section - as in your configuration you have `type="myNameSpace, myProject"`. How to you populate that type? – Oded Nov 13 '12 at 13:42
  • well, as a newbie I googled it and looked in msdn and found out that I should create a class that implemets ConfigurationSection. But I have no idea how should that class exactly look like. – bombo Nov 13 '12 at 13:44
  • See http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection – Oded Nov 13 '12 at 13:47
  • but I don't have the index of the city element, I only have the id – bombo Nov 13 '12 at 13:50
  • How you implement your configuration section is up to you. However, this does not sound like information you should have in _configuration_. It looks like something that belongs in a lookup (like a dictionary). – Oded Nov 13 '12 at 13:51

2 Answers2

1

You need a reference to the section:

var theSection = (TypeOfSection)ConfigurationManager.GetSection("mySection");

Note the cast to the TypeOfSection - this is the type declared in the config file.

At this point, you should have a strongly typed object that you can access and iterate over.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks for your answer, but I don't really get it. How do I iterate through mySection cities and given the id of the city be able to return the type (in this case nameSpace1, nameSpace2, or nameSpace3) – bombo Nov 13 '12 at 13:49
  • @Lina - In my answer, I assumed you have written a class implementing `ConfigurationSection` that has a collection of cities. – Oded Nov 13 '12 at 13:50
  • I'd highly appreciate it if you show me how that class should look like, I can't find an alternative anywhere in google that matches the configurationSection that I have – bombo Nov 13 '12 at 14:54
  • @Lina - of course you will not find exactly the same. Find something _similar_ and change it. – Oded Nov 13 '12 at 15:01
  • the proposed solution does not answer the question. – derloopkat Jul 17 '15 at 09:47
0

In my answer, I assumed you have written a class implementing ConfigurationSection that has a collection of cities. – Oded Nov 13 '12 at 13:50

This previous answer is technically correct although I feel it is lacking in detail. I will attempt to elaborate on @Oded response, hopefully it will be a clear exhibit!

Steps

  1. First thing to do here is to implement a new class, MyConfigSection which is just a standard POCO (Plain Old C Object) with properties for each configuration value that you will be storing in a configuration source.

    public class MyConfigSection
    {
        public City[] Cities {get; set;}
    }
    

    NOTE: We are using an array of objects which are type City, this is to allow for multiple entries as your specific use case requires, although you are not restricted to any given type. In this case the class would be structured something like follows:

    public class City
    {
        public string Id {get; set;}
        public string Type {get; set;}
    }
    
  2. Create the configuration section in the source of your choice and name the section something corresponding to the intended purpose, in this case we will call the section mySection to match the original question.

    <mySection>
        <Cities>
            <city Id="ny" Type="nameSpace1" />
            <city Id="dc" Type="nameSpace2" />
            <city Id="nj" Type="nameSpace3" />
        </Cities>
    </mySection>
    
  3. Now the key step is to bind the MyConfigSection poco to the desired configuration settings section. We can achieve this without having to go through the IOptions<T> interface by adding the following code to your startup class:

    var configSection = new MyConfigSection();
    Configuration.Bind("mySection", configSection);
    services.AddSingleton(configSection);
    
  4. After adding the binding, and registering the resulting object with the services container, it will now be available anywhere in you code base through dependency injection by simply including an argument of type MyConfigSection in any method where you require a value stored within the aforementioned configuration.

    public Type GetCityType(string cityId, MyConfigSection config)
    {
        var typeName = config.Cities
            .Where(x => x.Id == cityId)
            .Select(x => x.Type);
        return Type.GetType(typeName);
    }
    

    In this specific scenario, the use of the settings values is slightly more complicated by the fact that you are dealing with an array of an additional poco type, although with the help of LINQ it is made quite trivial to retrieve the necessary values from the Cities array. After you get the string which corresponds to the type and store it in the typeName variable, you can use reflection to return the actual Type object rather than just a string, as demonstrated above. (The GetType() method also has several overloads worth taking a look at, which allow you to configure what happens if a type is not found with the given name, and whether the search should be case sensitive, etc.)


I hope that this answer is clear and useful to your situation, if you have any questions that I did not fully answer, just leave a comment and I will be sure to update the answer with some additional elaboration!