So my situation is that I have an asp.net MVC project and I am looking to move most of the site specific settings to a config file that is not the Web.config. The reason for doing so is to avoid the app pool recycling when settings are changed, but I want to keep a lot of the configuration code I already have. So I've been trying to figure out a way to load a configuration file as a configuration object without it being the standard Web.config that loads with the application. I've been looking through some of the Configuration manager classes to load config files, but they all seem to want a path to which the class can find a machine, web, or app config. So is it possible to load a .config file (that is not web, app or machine) as a Configuration object? It seems like it should be pretty easy since its all just XML but I seem to be missing something.
Asked
Active
Viewed 368 times
1
-
possible duplicate of [Loading custom configuration files](http://stackoverflow.com/questions/505566/loading-custom-configuration-files) – CodeCaster Jul 16 '13 at 21:17
-
I think, it is a bit different issue but no doubt there is answer somewhere on SOF – T.S. Jul 16 '13 at 22:52
-
I've seen the methods as described by the answer to that question. I'm not sure that it works for my situation. Possibly it does, but I just may be misunderstanding the syntax. My config file is right alongside the Web.config in the file structure but those methods look to be used to load external configs from other sources. I couldn't figure out a way to just the
.config as a configuration object in .NET. – MattMacdonald Jul 16 '13 at 23:14
1 Answers
0
Configuration Changes Cause a Restart of the Application Domain
Changes to configuration settings in Web.config files indirectly cause the application domain to restart. This behavior occurs by design. You can optionally use the configSource attribute to reference external configuration files that do not cause a restart when a change is made. For more information, see configSource in General Attributes Inherited by Section Elements. MSDN
In your web.config:
<system.net>
<myconfigSection>
<myConfigEntry configSource="MyExtraConfig.config" />
</myconfigSection>
</system.net>
In your MyExtraConfig.config:
<?xml version="1.0" encoding="utf-8" ?>
<myConfigEntry>
<myConfigValue1 a="1" b="2" c="3" />
<myConfigValue2 d="4" e="5" f="6""/>
</myConfigEntry>

T.S.
- 18,195
- 11
- 58
- 78
-
Interesting, I'll look into this more. I already have the site specific configuration info set up in this way however I believe I've still experienced the behavior of the app pool recycling when I make changes. Maybe the default behavior is refresh on external change so maybe I need to explicitly set that to false, although .NET is preventing me from applying that parameter anywhere there is a configSource so maybe my setup is incorrect. – MattMacdonald Jul 16 '13 at 23:16
-
1So yes this information is correct. The way I had it set up was for it to restart on changes which I did not realize was the case. For anyone else that might be having this issue to edit the config I needed to open the file through a manager anyway and cast it to my ConfigurationSection class, make changes through setters, then save the config object the manager returned. – MattMacdonald Jul 17 '13 at 18:19