0

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 DbSettings, 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 DbSettings 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.

Geoff James
  • 3,122
  • 1
  • 17
  • 36
  • You're overcomplicating.. You need one connection string with one name, and different values in your different environments. – Jason P Aug 30 '16 at 21:41
  • Thanks @JasonP - I was thinking the same thing with the connection string, to be honest *sanity check*. However, connection string(s) aside; let's say I wanted to added a custom section with a collection in (like in OP), and then choose which key to refer to from an `` key. Is there any way I could do this? – Geoff James Aug 30 '16 at 21:52
  • 1
    Yes, creating a custom config section is a common problem, and there should be plenty of tutorials out there. Once you've done that, you can get the connection string using the variable containing the value: `ConfigurationManager.ConnectionStrings[ConnectionStringToUse]` – Jason P Aug 30 '16 at 21:56
  • Thanks again, @JasonP - I'll come back to the custom config sections when I've cleared my head a little. Cheers :) – Geoff James Aug 30 '16 at 21:59

1 Answers1

2

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 :).

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • Thanks for the suggestions @Sledgehammer! I'll come back to the custom sections with a clearer head and have a re-think of how I can address the deployment as well :) – Geoff James Aug 30 '16 at 22:15