How to Change .NET Configuration Files at Runtime (including for WCF)
来源:互联网 发布:网络配眼镜 编辑:程序博客网 时间:2024/04/30 14:32
One of the most common issues people run into with WCF configuration, and .NET applications in general, is that configuration files appear to be fixed. You only have one configuration file for an executable, and you can’t use different configuration files as your exe.config while the application is running. Of course, you can always shut down the application, change the configuration, and then restart it with the new configuration file, but that’s tedious and oftentimes undesirable. In this blog post, I’ll show you how to easily get around this limitation.
Suppose you have the following simple configuration file:
<configuration>
<appSettings>
<addkey="name"value="foo"/>
</appSettings>
</configuration>
And let’s say you have the following code:
public static void Main()
{
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
ChangeConfiguration();
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
}
static void ChangeConfiguration()
{
Configuration config =ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings.Clear();
appSettings.Settings.Add("name","bar");
config.Save();
}
The ChangeConfiguration method above opens up the configuration for the currently running executable, clears the appSettings dictionary, and then adds a new entry with value “bar” instead of “foo”. The main method simply checks the value of name before and after changing the configuration. If you run this code as is you’ll get the following output:
foo
foo
So even though you changed the configuration file, you’re not picking up on the appSetting’s new value. This is because the AppSettings section was cached when you first asked for it, and subsequent calls just use the cached version instead of reading from disk.
But now, if you add the following line of code to your Main method:
public static void Main()
{
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
ChangeConfiguration();
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
}
Making sure to revert the configuration file to its original value “foo”, you get the following output:
foo
bar
So by calling RefreshSection, you’re telling the configuration API that it should invalidate the cached AppSettings section and instead read from disk on the next configuration call.
This is very useful in particular for WCF because it allows you to use different configuration files for different services or clients, and then just have the same application use all of those configuration files. In this way, you avoid having large configuration files that merge together the information for very many services or clients. Unfortunately, you can’t RefreshSection on a section group, only a section, so you’ll typically have to use the following calls to refresh WCF sections:
ConfigurationManager.RefreshSection("system.serviceModel/behaviors");
ConfigurationManager.RefreshSection("system.serviceModel/bindings");
ConfigurationManager.RefreshSection("system.serviceModel/client");
ConfigurationManager.RefreshSection("system.serviceModel/services");
You may have to refresh other sections as well if you’re using some of the more advanced configuration sections.
Also worth noting: you don’t have to use Configuration.Save() to edit a configuration file, like I did above. You can change the file in any way you’d like, including changing the config file manually or using File.Copy().
Referenced from:http://blogs.msdn.com/b/youssefm/archive/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf.aspx
- How to Change .NET Configuration Files at Runtime (including for WCF)
- How to change size of WPF controls at runtime
- how to change dimension at view time
- how to get callstack at runtime?
- How to change sensor SW configuration from I2C to SPI
- Speech Recognition (1)-How to change wav files to array.
- How to Cheat at Configuring Exchange Server 2007: Including Outlook Web, Mobile, and Voice Access
- How to search for STUFF at google
- Change Locale at the runtime
- How to check permissions at runtime without throwing SecurityException?
- How can I add properties to an object at runtime?
- How to stop #including redundant headers?
- How to run 32 bit .NET Windows apps on 64 bit Windows (including with IEExec)
- How to change options for Format Fortran Indent Plugin ?
- How to change credentials for SVN repository in Eclipse?
- How to get the identity hash codes for files
- How to Collect Bne Log Files for GL Integrators
- 101 VB.NET Samples - How-To Configuration Settings
- 不得不知道的Unix/Linux的命令行神器
- bzero用法
- 关于ffmpeg 的总结(一个linux 下 集 屏幕录像录音,音频视频转换,合并音频视频文件,格式转换于一身的命
- linux 目录下文件应该怎样放
- linux里输入dbca时,出现exception in thread main
- How to Change .NET Configuration Files at Runtime (including for WCF)
- 一个经典实用的iptables shell脚本
- Spring JQuery/Ajax 进行 Json请求
- FFMPEG转码后得到的MP4必须要加载完才能播放的问题
- ARM定义特殊寄存器(*(volatile unsigned long *))的理解
- windows server 2008 R2/windows 7管理十驱动器映射及本地账户管理
- 解决Android的ListView控件滚动时背景变黑(转)
- c++: string 的函数replace()用法
- Android - LayoutInflater