0

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?

axel_c
  • 6,557
  • 2
  • 28
  • 41
  • Is the helper console program part of the same Visual Studio solution? If so you could consider building your config files with a NANT script. – RichardOD Sep 21 '09 at 08:15
  • The idea is to have a single config file to reduce maintenance cost and prevent future SNAFUs caused by differences in config files. – axel_c Sep 21 '09 at 08:53

3 Answers3

1

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
  • 10,324
  • 6
  • 55
  • 66
1

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
  • 94,805
  • 45
  • 217
  • 260
  • 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 Sep 21 '09 at 10:12
0

I know this is a very old question but you could consider storing the connection strings in a shared class library's app.config and using the method in the below SO answer to ensure that config file is always copied to the referencing project's bin folder as shared.project.dll.config. Then you just read shared.project.dll.config from the referencing project's bin folder to get the connection string.

Visual Studio/MSBuild copy referenced class library's app.config as *.dll.config to bin folder of current project

theyetiman
  • 8,514
  • 2
  • 32
  • 41