Custom config elements collection in Web.config

Intro

I'm developing a WebApp built on C# ASP.NET.

I've been researching creating a "Custom Configuration" section with child elements in the Web.config file, and I've hit a bit of a snag when it comes to consuming the keys/values in the data.

I seem to be going round in circles and I don't know how to tackle the issue I'm having.


Situation

I have a few different Connection Strings defined in the Web.Config file, in the <connectionStrings> section. They are for dev, test, and live databases.

<connectionStrings>
    <add name="connectionOne" connectionString="..." providerName="..." />
    <add name="connectionTwo" connectionString="..." providerName="..." />
    <add name="connectionThree" connectionString="..." providerName="..." />
</connectionStrings>

The WebApp is currently hard-coded to use one of these connection strings - if I need to change which one to use, I need to re-compile.


Desired Functionality

I'd like to define a section in the Web.config , let's say DbSettings .

In that, I'd then like to be able to define some child elements for, let's say DbSetting s, in which I could define dbConnectionName , foo , bar etc. as attributes.

For example:

<dbSettings>
    <dbSetting key="DbSetting1"
                     dbConnectionName="connectionOne"
                     foo="fooOne"
                     bar="barOne" />
   ... and so on
</dbSettings>

Then, perhaps in the <appSettings> section, define which of these DbSetting s elements I want to use to get the settings from:

<appSettings>
    <add name="dbSettingsKey" value="DbSetting1" />
</appSettings>

Desired Web.config section

Here is a fuller example of what I'd imagine my Web.config file to look like:

Connection Strings

<connectionStrings>
    <add name="connectionOne" connectionString="..." providerName="..." />
    <add name="connectionTwo" connectionString="..." providerName="..." />
    <add name="connectionThree" connectionString="..." providerName="..." />
</connectionStrings>

App Settings

<add key="dbSettingsKey" value="DbSetting1" /> // or 2, or 3 etc.

DbSettings (custom section)

<dbSettings>
    <dbSetting key="DbSetting1"
                     dbConnectionName="connectionOne"
                     foo="fooOne"
                     bar="barOne" />
    <dbSetting key="DbSetting2"
                     dbConnectionName="connectionTwo"
                     foo="fooTwo"
                     bar="barTwo" />
    <dbSetting key="DbSetting3"
                     dbConnectionName="connectionThree"
                     foo="fooThree"
                     bar="barThree" />
</dbSettings>

My question...

How the devil am I going to get this desired functionality in the C# code?

I've read loads on "creating your own custom section", and similarly "creating a custom config collection". But, I just can't seem to glue it all together to apply for my situation.

I'd like to be able to have a class (like the one I'm using at the moment with the hard-coded strings), which I can reference necessary properties (as I am doing, at the moment) - and then the code can dynamically load the correct settings at run-time from the sections I've described above.

As always, thank you in advance for your suggestions and help.


I agree with the comments. The way this is usually done is you deploy a different web.config to each environment. When your deployment group (or you) deploys, you deploy everything EXCEPT the web.config unless you have changes to push.

In answer to your other question, adding a custom section is not trivial. It's quite a bit of work. Custom section handler which requires a whole bunch of configuration element classes and a bunch of configuration element collection classes... and then, if you want it to "work" correctly, you also need to create a schema and register that with the IDE, etc.

For your particular case, I'd just do it the "normal" way :).

链接地址: http://www.djcxy.com/p/42594.html

上一篇: Android ScaleAnimation似乎忽略了数据透视

下一篇: Web.config中的自定义配置元素集合