Question

I have a website with a connection string listed in its web.config. The connection string is altered by the Publish feature so that it can reference a development database until it is released, when it references a separate release database.

The website accesses the databases through some assemblies, though. They are Class Libraries so they can't be Published, at least as far as I can tell. I read that the web.config would override the app.config connectionstrings, but that doesn't seem to be happening.

Whenever I Publish the release site references the development database, unless I alter the assemblies app.config files to reference the release database.

I don't want to have to remember to do that every time. How do I handle this?

Was it helpful?

Solution

You've got two issues here:

1. How to remember to publish the correct settings each time you publish:

One way to deploy such settings is by using web.config transformations in Visual Studio. This is pretty easy to set up and means that you do not have to remember to update the settings each time you publish.

As well as debug and release environments, you can also create transforms for "UAT", "Staging", "Beta" or whatever other configs you might need.

You might find these articles useful: here, here and here.

For example, here is a transform for a Release environment:

    <?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
    <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
  </system.web>
</configuration>

Above, you will see that the transform for Release mode sets the attributes of the MyDB connection string (xdt:Transform="SetAttributes"), removes the debug attribute from the compilation section and replaces the customErrors section with a new version.

It's a simple, yet very powerful technique.

2. How to get your assemblies to pick up the settings in the web.config

If your libraries have been written in the usual way, they should be retrieving their connection strings by simply accessing the [Web]ConfigurationManager.ConnectionStrings property. Like @Bob Horn says, they should then pick up the settings from the host process's config file (in this case the web.config of your web app).

However, sometimes you might find that a library is getting its settings from a .Settings file in the project, in which case things get a little more complicated. You will need to copy the settings section of the app.config in to the web.config. (You can also do this using the transforms technology described above.)

If you have access to the source code of the other assemblies, find the part of the code that retrieves connection strings. If it's not accessing the ConfigurationManager class, then that might explain why it's not picking up the web.config file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top