views:

400

answers:

2

I have a web deployment project that does a web.config section replacement using an external file. (this is to change the connection strings section). The web.config section replacement works fine when built manually, but when built as part of a TFS build the section is not replaced. I cannot find any errors or warnings in the build log.

What are the likely causes, or how can I 'debug' this?

A: 

I am not sure this will help at all....but this is a way to add/update a connection string without having to replace the whole config section.

  public static void SaveConfigVal(string connectionString, string connName)
        {

            System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = GetConfigFileName();
            //System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            // Retrieve the section group
            ConnectionStringSettings keyValue = config.ConnectionStrings.ConnectionStrings[connName];

            // If the key already exists, just replace
            if (keyValue != null)
            {
                keyValue.ConnectionString = connectionString;
            }
            else
            {
                // Add a new key if the setting doesn't exist
                config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(connName, connectionString));
            }

            config.Save(ConfigurationSaveMode.Modified);// (ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }

        private static string GetConfigFileName()
        {
            //return config file name....
        }
CSharpAtl
How are you integrating this with a build or web deployment?
simon831
Are you using an MSI?
CSharpAtl
+1  A: 

Have you considered using Web.Config's ability to pull a section from a separate file? You refer to the external file like so (this is my code for loading a file that has my connection strings section):

<connectionStrings configSource="WebCS.config"/>

Then the connection string can be deployed as a separate file:

 <connectionStrings>
       <add name="ConnString" connectionString="Data Source=<server>;Initial Catalog=<DB>;User ID=<ID>;Password=<pwd>" providerName="System.Data.SqlClient"/>
 </connectionStrings>

That way, you don't have to worry about changing the web.config file at all.

Mark Brittingham