Custom configuration in .NET

2019-04-12 09:28发布

问题:

I am seeing simple examples regarding custom configuration in .NET. My case is a bit more complex, with nested nodes.

I'd like to be able to read this from the configuration file:

 <environments>

<environment name="live" url="http://www.live.com">
  <server name="a" IP="192.168.1.10"></server>
  <server name="b" IP="192.168.1.20"></server>
  <server name="c" IP="192.168.1.30"></server>      
</environment>

<environment name="dev" url="http://www.dev.com">
  <server name="a" IP="192.168.1.10"></server>
  <server name="c" IP="192.168.1.30"></server>
</environment>

<environment name="test" url="http://www.test.com">
  <server name="b" IP="192.168.1.20"></server>
  <server name="d" IP="192.168.1.40"></server>
</environment></environments> 

If anyone could provide some code for that, I'd appreciate it.

Thanks!

回答1:

You can read this by implementing custom configuration classes inheriting from the ConfigurationElement class.

Here is an example of the "server" element:

public class ServerElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return ((string)base["name"]); }
        set { base["name"] = value; }
    }

    ...
}

The environment element is actually a collection and could be implemented like this:

public class EnvironmentElement: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return new ServerElement(...);
    }
}


回答2:

A nifty tool I came across recently is the Configuration Section Designer up on CodePlex; integrates into VS nicely.

Generates classes, XML schema, etc. Recommended.



回答3:

Your need to create some classes to handle the custom configuration sections of the config file.

There's a very good blog entry by Phil Haack on this: Custom Configuration Sections in 3 Easy Steps.

Edit:

I was trying to find the Code Project article I've used in the past to learn how to achieve this, and I've found it on Phil's blog entry:

here it is Unraveling the Mysteries of .NET 2.0 Configuration

it contains the info necessary to handle nested elements and collections.



回答4:

This example might help : To create a custom configuration section handler (MSDN)



回答5:

Looking at the content of your example (targeting different staging environments), rather than reading config files dynamically, you might want to look at web deployment projects, as indicated in the answer to an earlier question.

They handle config merging at deployment time instead: a good idea, especially if you have sensitive information like connection strings with service account passwords...



回答6:

I would use App.Config file

System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];

http://www.eggheadcafe.com/community/aspnet/2/10004734/appconfig-file-c.aspx



回答7:

Here is what I implemented recently after reading this article:

Using Reflection for binding classes to .ini files

http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!174.entry