views:

2567

answers:

9

Has anyone got this working in a web application?

No matter what I do it seems that my appSettings section (redirected from web.config using appSettings file=".\Site\site.config") does not get reloaded.

Am I doomed to the case of having to just restart the application? I was hoping this method would lead me to a more performant solution.

Update:

By 'reloading' I mean refreshing ConfigurationManager.AppSettings without having to completely restart my ASP.NET application and having to incur the usual startup latency.

A: 

The App.Config settings are cached in memory when the application starts. For this reason, I don't think you'll be able to change those settings without restarting your application. One alternative that should be pretty straight forward would be to create a separate, simple XML configuration file, and handle loading/caching/reloading it yourself.

Terrapin
So what is http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx for?
Kieran Benton
Good question - I don't know :)
Terrapin
A: 

You have to save the date before calling RefreshSection and your ASP.Net app needs writing permissions on the web.config file in order to do that.

sebastian
A: 

This seems to be a flaw (maybe a bug) when using an external config file for your appSettings. I've tried it using the configSource attribute and RefreshSection simply never works, I'm assuming this is the same when using the file attribute. If you move your appSettings back inside your web.config RefreshSection will work perfectly but otherwise I'm afraid you're doomed.

A: 

To write, call it this way:

Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")

Return AddOrUpdateAppSetting(config, "YourSettingKey", "YourValueForTheKey")

To read and be sure you get the values in file, instead of those in cache, read it this way:

Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~")
  Return config.AppSettings.Settings("TheKeyYouWantTheValue").Value

Full example:

Protected Shared Function AddOrUpdateAppSetting( _
       ByVal Config As System.Configuration.Configuration _
     , ByVal TheKey As String _
     , ByVal TheValue As String _
     ) As Boolean</p>

    Dim retval As Boolean = True

    Dim Itm As System.Configuration.KeyValueConfigurationElement = _
        Config.AppSettings.Settings.Item(TheKey)
    If Itm Is Nothing Then
        If Config.AppSettings.Settings.IsReadOnly Then
        retval = False
        Else
        Config.AppSettings.Settings.Add(TheKey, TheValue)
        End If


    Else
        ' config.AppSettings.Settings(thekey).Value = thevalue
        If Itm.IsReadOnly Then
            retval = False
        Else
            Itm.Value = TheValue
        End If


    End If
    If retval Then
     Try
        Config.Save(ConfigurationSaveMode.Modified)

     Catch ex As Exception
        retval = False
     End Try

    End If

    Return retval

End Function
+1  A: 

Make sure you are passing the correct value to RefreshSection, i.e.

ConfigurationManager.RefreshSection("appSettings");
gWiz
A: 

Have you tried storing your AppSettings in its own external file?

From app.config/web.config:

<appSettings configSource="appSettings.config"></appSettings>

appSettings.config:

<?xml version="1.0"?>
<appSettings>
  <add key="SomeKey" value="SomeValue" />
</appSettings>

Changes made to appSettings.config should be reflected instantly. More info: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx

Vince
A: 

Yes. you are stuck with iis restarting.

There is a feature with asp.net 4.0 and iis 7.5 where the initial startup is removed.

yamspog
A: 

I am not sure if this is possible in a web app, but it works in a desktop app. Try using ConfigurationSettings rather than ConfigurationManager (it will yell at you for using outdated classes...), then reading all the data into a class. When you wish to refresh, simply create a new instance and drop all references to the old instance. My theory for why this works (might be wrong): when you don't directly access the app.config file the entire time you are running, the file lock is dropped by the application. Then, edits can be made when you are not accessing the file.

badpanda
A: 

Problem with me is that i am my app.config file is not be modified, i am using this code

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings.Remove("SQLServerVersion");
            config.AppSettings.Settings.Add("SQLServerVersion", conn.SqlVersion);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

Regards Nauman Eqirtas

Nauman
Is this a question?
Kieran Benton