Single App/ConnStrings Config File for Multiple Applications

clamum

Lifer
Feb 13, 2003
26,255
403
126
I have been assigned to investigate how to implement a single point of storage for application & connection string configuration name/value pairs for multiple .NET applications. My first thought was a SQL Server database table, which could be used perhaps, but the problem is that connection strings could not be stored in it because they are first required to connect to the database itself.

At a previous job we did use external web.config files for a web application, for switching between Test/Prod I believe, by doing "<connectionStrings file="/ConnectionStrings.config" />" in the web.config and having external "ConnectionStrings.config" files.

Would this approach work for multiple .NET apps as it does for just a single web app? For example, a Windows app, Console app, and Windows Service app all sharing a single app.config file and a single connection strings config file? Any other recommendations?

Thanks for any insight!
 
Last edited:

Savatar

Senior member
Apr 21, 2009
230
1
76
This sounds like it should work without a problem, or even just store a configuration file in a standard path (either hard-coded, passed via command-line, or in its local config file) and load with System.Configuration.ConfigurationManager:
http://stackoverflow.com/questions/...ger-to-load-config-from-an-arbitrary-location

The registry is always a possible too, though I hear that Microsoft is discouraging its use, in general. Those are two possibilities.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
It definitely will work, if only because as Savatar said you can load and parse .configs manually without too much trouble.

Another option might be to use a redis instance as a centralized key/value store. You can secure the endpoint and keep a copy of the dump.rdf file as a backup of your configuration.
 

clamum

Lifer
Feb 13, 2003
26,255
403
126
Thanks for the replies guys. Well I started by creating a solution and adding a Console Application and a Web Site to it. I put two files in C:\, named "SingleAppConfig.config":

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings file="/AppConfig.config"/>
</configuration>
and "AppConfig.config":

Code:
<appSettings>
	<add key="TestAppKey" value="this is a test value" />
</appSettings>
I originally tried to use the "AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\SingleAppConfig");" route to implement this, but that did not work (worked fine in the Console app, Web App couldn't read it). So I instead tried Gavin's second solution from Savatar's link and this worked great:

Code:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = @"C:\SingleAppConfig.config" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection appSettings = config.GetSection("appSettings") as AppSettingsSection;
var config = appSettings.Settings as KeyValueConfigurationCollection;
lblTestAppKey.Text = config["TestAppKey"].Value;
This allowed me to retrieve values from the SingleAppConfig.config file from both the Console and Web App, and additionally use the local Web.config in the Web App as well. AppSetting key/values that are in my Machine.config are also loaded into the AppSettingsSection in the above code. So now I just want to move this functionality into it's own class library and do some tidying up, but the basic functionality works.

Just thought I'd update with my solution in case someone else happens to find it helpful. Thanks again for the help guys.
 

LokutusofBorg

Golden Member
Mar 20, 2001
1,065
0
76
I've worked on systems that stored all environment-specific info in a database table, as you mentioned. You just have to configure the correct connection string for the environment (dev, test, prod, etc.), also as you mentioned. However, it's a bit of work keeping the tables updated with the correct values for the right environments when you have a complex deployment model (dev, test, prod, etc.) or even worse - customer site deployments. So I don't really favor using the database for this kind of stuff.

At another job, we did a centralized file-based config in our Core assembly, and I really liked how it worked. It was a bit of a bear figuring out how to get Visual Studio/MSBuild to copy the "sidecar" config files (that's what they're called) into the bin folder of any projects that referenced Core, but we got it working. I can share that code/config if you need.

For the centralized config files, we used ExeConfigurationFileMap as you have come up with. We used a custom XML schema for simplicity sake by sub-classing ConfigurationSection, ConfigurationElement, et al. We then wrapped it in a static class called EnvironmentSettings and exposed each setting as a strongly-typed property.

That let us have XML like this:

Code:
<configuration>
  <configSections>
    <section name="customSettings" type="Core.SystemConfig.CustomSettingsSection, Core.SystemConfig" />
  </configSections>
  <customSettings>
    <someCustomSettingName>Any basic value type value here...</someCustomSettingName>
    ...
  </customSettings>
</configuration>

The property mapping inside our ConfigurationElement derived class looks like this:

Code:
[ConfigurationProperty("someCustomSettingName", IsRequired = true)]
internal TextElement SomeCustomSettingName { get { return (TextElement)this["someCustomSettingName"]; } }

The wrappers to strongly-type the values in EnvironmentSettings then look like this:

Code:
// for a string value
public static string SomeCustomSettingName { get { return CurrentCustomSettings.SomeCustomSettingName.ToString(); } }

// for a bool value
public static bool SomeCustomSettingName { get { bool result = false; return Boolean.TryParse(CurrentCustomSettings.SomeCustomSettingName.ToString(), out result) && result; } }

(CurrentCustomSettings is a static field used to store the loaded config from the ExeConfigurationFileMap part of things.)

And code that called it simply looks like this:

Code:
var someSettingValue = EnvironmentSettings.SomeCustomSettingName;

I don't know if that will help you any, but there it is... Let me know if you have any questions.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |