Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share application configuration in a .net application?

I've got a relatively large .Net system that consists of a number of different applications. Rather than having lots of different app.config files, I would like to share a single configuration file between all the apps.

I would also like to have one version when developing on my machine, one version for someone else developing on their machine, one version for a test system and one version for a live system.

Is there an easy way of doing this?

like image 837
Nick Randell Avatar asked Sep 30 '08 21:09

Nick Randell


People also ask

How do I add an application configuration file to a project?

To add one to your project, right-click the project in Solution Explorer, Add..., New Item... and pick "Application Configuration File" from the Templates box. Show activity on this post.

How do I add configuration to a dotNET console application?

Settings files, such as appsettings.json New .NET console applications created using dotnet new or Visual Studio by default do not expose configuration capabilities. To add configuration in a new .NET console application, add a package reference to Microsoft.Extensions.Hosting. Modify the Program.cs file to match the following code:

How do I add additional configuration settings to an app?

Add as many common configuration settings as required within the <appSettings> node using <add> elements the same way as you would with any normal app.config file. Save the configuration file.

How to use the same app config file in multiple projects?

Step 1: Place the App.Config file in a folder parent to both (or all) the projects that needs to use the same Config file. I have put it in a new folder called AppConfig. Step 2: Right click on each project and select the same App.config file but add it as Link. You can see how to get the link from the image :


1 Answers

For large amounts of configuration which is needed by multiple applications, I would put this configuration into a central repository, e.g. a database, file in a common location.

To use different versions of a configuration file for different environments, create a build configuration for each of the different environments and a config file named after the environment, e.g:

production production.app.config test test.app.config

You can then use a pre build event to copy the correct config over the default app.config in your project. This will then get copied to your output directory as normal.

The pre build event would look similar to the above, just use $(Configuration) to get the appropriate file for the environment you want.

You could combine this with the above to copy the overall build specific config files into each project.

like image 161
marcj Avatar answered Sep 17 '22 05:09

marcj