views:

47

answers:

3

What is the difference between configuration.Save(ConfigurationSaveMode.Modified, true) and configuration.Save()?

Background: I have a programme, where I manipulate a web.config, which I use for configuring WCF Services. I load it into a Configuration object, change some attributes and save it back. When I use configuration.Save(ConfigurationSaveMode.Modified, true) I get an Exception like this:

"It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level..."

When I use configuration.Save(), then it works! The reason for the exception may be the section <serviceActivations> in my web.config (the exception points to this section)

+1  A: 

The default parameters to Save are:

Save(ConfigurationSaveMode.Modified, false);

So the only difference would be that you force saving the configuration, even if it was unchanged. See http://msdn.microsoft.com/en-us/library/ms134089.aspx for more information.

Pieter
+1  A: 

Why woyld you write configuration.Save(ConfigurationSaveMode.Modified, true) when:

Isn't the first option the opposite of the second?

GôTô
A: 

ConfigurationSaveMode.Modified only saves the parts of the configuration that are different to the application/system configuration to a user local or roaming configuration (i.e. using ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel) with ConfigurationUserLevel.PerUserRoaming or ConfigurationUserLevel.PerUserRoamingAndLocal).

Since ASP.NET doesn't have user levels (and user isolated storage) this doesn't make sense.

From the documentation is not clear if any of the Configuration.Save overloads will really work in the case of ASP.NET which uses a completely different configuration setting inheritance model to non-ASP.NET .NET applications. In practice using one of the WebConfigurationManager to load the configuration manager is likely to be a necessary pre-condition to saving the file.

Another approach might be to explicitly load a explicitly designated file with ConfigurationManager.OpenMappedExeConfiguration.

Richard