ConfigurationManager.RefreshSection... Really working?


  • I need to "change" between diferent web.config depending on the operation i'm going to do next (for example, i load another assembly and execute a method of that assembly, thus i need to prepare the environment for that assembly).

    I've tried merging my main config with the other one (using the provided configuration class and respective subclasses, a complete mess) and I also tried to replace my current web.config and use refreshsection on every section that exists in the new configuration, but still i got no result.

    I though on creating a new AppDomain for each Assembly i'm going to load, but maybe that's too much mess and even doesn't works (because a different assembly might have diferent levels of web.config, so the configuration file would need to be changed every time i changed the virtual path i'm accessing and they would need to be merged - something I haven't achieved so far).

    The title of this question is related to what RefreshSection says it'll do "Refreshes the named section so the next time that it is retrieved it will be re-read from disk.", as it looks the most promising solution to this - because i could merge both configuration files by merging it's xml, but there's no point in doing so unless i can really refresh or change the web.config -.

    EDIT: I'm using .NET 4.0 with ASP.NET MVC 3.

    • Edited by yushir Monday, April 09, 2012 11:36 AM
    • Moved by Mike Feng Monday, April 16, 2012 2:32 AM (From:.NET Base Class Library)
    Friday, April 06, 2012 6:56 PM

All replies

  • Hi Yushir,

    Welcome to the MSDN Forum.

    Please take a look at this article: 

    ASP.NET detects changes to configuration files and automatically applies new configuration settings to Web resources affected by the changes. The server does not have to be rebooted for the changes to take effect. Hierarchical configuration settings are automatically recalculated and recached whenever a configuration file in the hierarchy is changed. The <processModel> section is an exception.

    So when your config file indeed changed, your application will update the config cache.

    Best regards,

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 09, 2012 7:58 AM
  • It should be that way, also there are reports that on ASP.NET 2.0 the application was restarted and thus you needed to stop file monitoring, but believe me it doesn't work.

    Just a simple example: Create a new web application, for example a MVC 3 with intranet auth, and copy it's web.config file. Now add a new service reference, just that a new entry on system.serviceModel is added with it's endpoint information and create a new instance of the client. Then, rename this new web.config file and restore the original one.

    When you run the application it'll throw an exception when it tries to create a client because it doesn't have any endpoint reference, but that's the expected behaviour. Now, if you programatically swap files while executing the application, just before the call to create the new client, it'll throw the same exception, because it hasn't been able to find the endpoint.

    This leads me to three conclusions: a) file monitoring is not doing it's job well and thus it's not detecting the change on web.config (in some forums say that the expected behaviour when a change is detected is to restart the application using thread.abort(), but that's maybe on 2.0), b) file is cached and needs to be refreshed (that'd justify the role of RefreshSection(name), but still it doesn't work), c) and last, is because the application is not on a Production Environment, ie because it's running under an ASP.NET development server but i refuse to believe this might be the problem.

    Monday, April 09, 2012 11:47 AM
  • Hi Yushir,

    How about posting this issue on forum: ?

    Best regards,

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 10, 2012 1:47 AM
  • Hi,

    I still think that this issue is more related to .NET base framework than, but anyways i'll post it there too.


    Friday, April 13, 2012 12:38 AM