views:

70

answers:

2

I've created a class library that contains three entity models for three databases that other classes have to connect to. This class library is then used by three different web services and a website. These are all installed on their own server so there's no easy way to let them share the same connection data.

In my web.config files I have this line:

<connectionStrings configSource="Dependencies.ConnectionStrings.Config"/>

This will load the connection strings from a separate XML file. It works fine but it also means that I now have 5 different copies of this Dependencies.ConnectionStrings.Config file. I would prefer to use a single file during development.

However, my solution also contains four setup projects and in these setups, the config files must be next to each other.

So how do I tell VS to share a single config file between multiple projects yet without modifying web.config?


To make it slightly more complicated, the class library uses an app.config to store the connection strings. And I can't tell app.config to link the connection strings to a separate config file, apparently... One additional complication is that the connection strings need to be modified by the system administration after installation. That's because it will have to look to a different database than the development databases. (Actually, during development, I use 3 databases to split up the entity logic. The Test and Production environments will just use a single database.)
My current solution is simple. I've added the Dependencies.ConnectionStrings.Config file to a separate folder in my solution and regularly drag&drop this into the other projects to update them. Although this works, it's error-prone...

+1  A: 

The big issue is you cannot have a linked config file in the folder hierarchy above the current main config file, which makes sharing them difficult.

Have you looked at placing this somewhere higher up the configuration hierarchy- perhaps in the root web.config or machine.config?

Another option is to have some pre build events build these files, so you don't have to worry about having multiple copies of them. I've used the prebuild option quite successfully when I need to create a different connection string per developer.

RichardOD
The root web.config would be a good location too, but the connection strings will sometimes change during development and testing. Modifying a simple XML file is just a bit easier for the testers, who don't have any experience with IIS itself. (Which is why I need to create setup's.)
Workshop Alex
+1  A: 

I don't know if it's possible to do what you're asking without modifying the file.

I agree it would be much better if you had one file that could be used by all servers, then you won't have versioning problems, etc.

To be honest, the whole setup sounds pretty complicated and perhaps prone to error if say the wrong config file ended up on the wrong server.

I would be tempted to centralise the connection info in a shared database. You could record the connection strings that each server is supposed to use (stored against server name). This way each server would know which connection strings it sould use. This of course assumes that your servers could all contact one central database server (depends on your setup).

Of course there are other ways of getting the desired result, but I think you could get a much more elegant solution if you "roll your own" rather than trying to bend the inbuilt app settings to do what you want.

DoctaJonez