0

We are a group of C#/.NET 4.5 developers working on the same application.

The application has a set of configurations related to each developer machine, like the connection string to the DB, network related settings (proxies, IPs, credentials) and a LOT MORE.

Because the application has grown we are incurring in a lot of environment related configurations like for example:

  • If this is MyPC then load the connection string for my PC.
  • If this is the XDeveloperPC then specify proxy’s settings.

Also if new developers leaves or join the group, then the process to update the file becomes a total head ache. Maintaining the file has become very hard and is a possible source of bug and errors.

I was thinking in having specific app.config files related to each developer environment like:

  • app_MyPC.config
  • app_XDeveloperPC.config

And when the application builds or executes then specify which one to load as if it where the default app.config of the application. Then, when the application or any class or method refers to a given configuration (like the connection string) is access to this configuration file as if it where accessing to the app.config default file.

I would not want to create a Configuration class that builds immediately when the application starts because then I should have references from every place to this class and the application is quite large, with a bunch of projects and dlls. I rather prefer to hear some opinions and what do you think should be the best way to achieve this.

Is it possible to achieve this? How? Do you know a better approach?

mdarefull
  • 829
  • 2
  • 14
  • 24
  • Take a look at using [User Settings](https://msdn.microsoft.com/en-us/library/bb397750%28v=vs.110%29.aspx). It will involve changes to use a centralised config class, but it's probably better in the long run. – James Thorpe May 18 '15 at 10:23
  • Sounds like you *should* be looking at ways to centralize configuration management, especially if you want administrative access. Loading per-user configuration settings from a database would make managing them a lot easier – Panagiotis Kanavos May 18 '15 at 10:37
  • 1. Store in source control App.config with default values. 2. Use settings instead app.config wherever it is possible. 3. If you have to change local app.config just don't commit it into source control. – Dennis May 18 '15 at 10:37
  • 1
    For limited scenarios, you can use [XML Transformations](https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859) to apply changes over an existing app.config. This works when you need a limited number of environments, eg debug, production, test. It's used in ASP.NET to specify different configs per environment – Panagiotis Kanavos May 18 '15 at 10:40
  • Or environment variables with defaulted values from app.config – Matt Oct 21 '22 at 11:59

3 Answers3

0

FYI, please note that .NET only loads one config file for the whole application. You could try multiple config files something as like specified here,

Multiple App.Config Files in .NET Class library project

Hope this helps...

Community
  • 1
  • 1
Vanest
  • 906
  • 5
  • 14
0

You can specify sections of app.config to be loaded from another file. See this answer

However, you might have to customize the above solution, the app.config files and the way configs are organized.

Another approach is to use a custom settings file instead of app.config. This will need some code change to use the config file. This config can either be an XML or a JSON file (JSON is easy to deal with). You can use an inheritance model wherein generic settings come from one file, specific settings come from another file and so on. Also, you can load settings from such file at runtime and change application behavior on the fly.

If you decide to use custom config file, and if you already have lot of code written considering App.config file, you can abstract the app.config calls to a method and let that method deal with where to pull the settings value from. That way you can minimize the code change and keep everything tidy.

Community
  • 1
  • 1
Amit
  • 25,106
  • 25
  • 75
  • 116
-2

Maybe you can use the machine.config file (C:\Windows\Microsoft.NET\Framework\your Framework version\Config\machine.config)

Karedel
  • 11
  • 5
  • That's not a possibility, in that case I should have to include all the connection strings for all the application I'm developing and so on, also if that file get broken then all my environment will suffer. – mdarefull May 18 '15 at 10:36