System.Configuration:有关 Configuration.Save 方法的问题
configuration.Save(ConfigurationSaveMode.Modified, true)
和 configuration.Save()
之间有什么区别?
背景:我有一个程序,在其中操作web.config
,我用它来配置WCF 服务。我将其加载到 Configuration
对象中,更改一些属性并将其保存回来。当我使用configuration.Save(ConfigurationSaveMode.Modified, true)时,我得到一个Exception
,如下所示:
“使用注册为allowDefinition =的部分是错误的'MachineToApplication' 超出应用程序级别...”
当我使用 configuration.Save()
时,它就可以工作了!异常的原因可能是我的web.config中的
部分(异常指向此部分)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保存的默认参数是:
所以唯一的区别是您强制保存配置,即使它没有更改。请参阅 http://msdn.microsoft.com/en-us/library/ms134089。 aspx 了解更多信息。
The default parameters to Save are:
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.
为什么要在以下情况下编写
configuration.Save(ConfigurationSaveMode.Modified, true
):仅导致修改的属性
写入配置文件,
即使该值与
继承的值。
配置未修改;
否则为 false。
第一个选项不是与第二个选项相反吗?
Why woyld you write
configuration.Save(ConfigurationSaveMode.Modified, true
) when:Causes only modified properties to
be written to the configuration file,
even when the value is the same as
the inherited value.
configuration was not modified;
otherwise, false.
Isn't the first option the opposite of the second?
ConfigurationSaveMode.Modified
仅将与应用程序/系统配置不同的配置部分保存到用户本地或漫游配置(即使用ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel)
与ConfigurationUserLevel.PerUserRoaming
或ConfigurationUserLevel .PerUserRoamingAndLocal
)。由于 ASP.NET 没有用户级别(和用户隔离存储),因此这是没有意义的。
从文档中不清楚是否有任何
Configuration.Save
重载在 ASP.NET 中确实有效,它使用与非 ASP.NET .NET 应用程序完全不同的配置设置继承模型。在实践中,使用WebConfigurationManager
加载配置管理器可能是保存文件的必要先决条件。另一种方法可能是使用 ConfigurationManager.OpenMappedExeConfiguration 显式加载明确指定的文件。
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. usingConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel)
withConfigurationUserLevel.PerUserRoaming
orConfigurationUserLevel.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 theWebConfigurationManager
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.