views:

210

answers:

3

Suppose I have an ASP.NET website running with its corresponding web.config file, which contains a bunch of connectionstrings for database access. I also have a little helper console program (deployed to the same folder as the web app) that does maintenance tasks and other stuff. This helper programs shares a bunch of dll's with the web app, so i have to keep a separate bla.exe.config file for it so the shared dll's find the connection strings and etc. I want to be able to make the console app use the web.config instead of having its own config file.

So far I've managed to load the appSettings at runtime (using ConfigurationManager.OpenMappedExeConfiguration, looping through the appsettingts and adding them dynamically to the ConfigurationManager.AppSettings collection), but the ConfigurationManager.ConnectionStrings member is apparently read-only so i cannot 'inject' the web.config's connectionstrings into it.

Any ideas?

+1  A: 

Personally I would just program a way so that both applications can find the same file. I would use something like Environment.SpecialFolder using the CommonApplicationData enumeration. You might consider the machine.config file though it's generally not recommended.

Richard Nienaber
+1  A: 

You could try to use the configSource attribute of the connectionStrings section, to reference an external file. I haven't tried it, but something like this:

<connectionStrings configSource="..\connnectionstrings.config">

connectionstrings.config:

<connectionStrings>
  <add name="..." connectionString="Server=...;Database=..;" />
</connectionStrings>


Update:

The approach shown above does not work. The configSource element does not support absolute paths, or relative paths starting with "..\".

M4N
I tried the configSource approach and it seems to work. Problem is, i have to place the 'connectionstrings.config' file directly under the 'bin' directory, which kind of sucks, but is still acceptable. I'll wait to see if a better solution comes up, otherwise i'll mark this as accepted.
axel_c
+1  A: 

Check out this link and this link.

astander