views:

3318

answers:

7

I have two applications one a console application and the other an ASP.NET app. They both need to know the same appSettings and connectionStrings. So ideally I would like to use the configSource property of app.config/web.config files to point that to a central location. For example

<connectionStrings configSource="D:\connectionStrings.config"/>
<appSettings configSource="D:\appSettings.config"/>

That however fails with an error:

The configSource attribute is invalid.: The configSource 'D:\appSettings.config' is invalid. It must refer to a file in the same directory or in a subdirectory as the configuration file.

Is there anyway to still use the configuration managers appSettings/connectionStrings and get the values from an external location?
I'm happy with having to add code to do it, but I don't want to have to replace the entire configuration manager system.

+1  A: 

You can place both settings in the machine.config and then they are available for all you applications on the server.

freggel
It's an option but I don't want (need) the settings available to other applications on the machine. Worry is the connection strings, they have pretty generic names which may conflict with other applications.
Robert MacLean
+1  A: 

You can load configuration from an arbitrary location, but it won't be available via the static properties of ConfigurationManager:

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(path)

(There is an overload that allows multuple files to be specified, to support default/user-roaming/user-local hierarchy.)

Losing the static properties means all the code needs to be aware of the different configuration.

Richard
Robert MacLean
The whole config is loaded by the normal (static) properties anyway so this makes no real difference.
Richard
Also, you could just use your own XML format and add its name to the applications' configuration files and read it directly.
Richard
+2  A: 

It seems that's that is the way it is. configSource must be in the same folder or deeper.

You could, although I'm not sure you should, use an NTFS hardlink. [mad grin]

Iain M Norman
+1 for out of the box thinking that solves it partially ;)
Robert MacLean
A: 

The solution I found worked best was to put the "shared" config files in a central files and then use a pre-build event in Visual Studio to copy them to a relative folder of each project which needed it.

Robert MacLean
+5  A: 

Another solution is simply to add the configuration file in all your projects as a link instead of actually copying the file to your projects. Then set the "Build Action" of the file to "Content" and "Copy to Output Directory" to "Copy if newer" and when you compile the project you will have the file in the output directory.

To add the file as a link choose "Add as link" in the "Add Existing Item" dialog.

nice - much easier for people to understand than my way
Robert MacLean
Funka
Of course only right after I decide to break down and post a comment asking for help do I then figure it out right afterward. I noticed my db.config is _also_ copied to the /bin/ folder, so I've updated my web.config to prepend this path in its `configSource` and all seems well. Thanks again!
Funka
A: 

Under appSettings you can use file= instead of configSource=

A: 

I had quite a struggle with this issue, but I found a good solution for it here: test run with external config

(You can direct the test run to copy files and directories into the test run directory by editing the .testrunconfig file.)

Although why the unit test type project can get config settings from its own app.config, but not be able to load referenced config files like a normal app.config is kind of baffling to me. I'd call it a bug because you would expect a test project app.config to behave the same way that the application's app.config behaves, but it doesn't.

marcel_g