博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在WCF程序中动态修改app.config配置文件
阅读量:4493 次
发布时间:2019-06-08

本文共 5005 字,大约阅读时间需要 16 分钟。

 

今天在个WCF程序中加入了修改配置文件的功能。我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google搜了一下,在园子里的文章中找到了解决方案。

原来,.net framework中对于配置文件不是实时读取的,而是有缓存的。对于那些已经更新了的内容,需要调用ConfigurationManager.RefreshSection(需要添加System.Configuration.dll的引用)函数刷新相应节点。

比较蛋疼的是,这个函数并不支持刷新Group。也就是说,我们不能通过ConfigurationManager.RefreshSection("system.serviceModel")一句话实现对WCF的配置刷新,需要调用如下四句话才行。

ConfigurationManager.RefreshSection("system.serviceModel/behaviors");ConfigurationManager.RefreshSection("system.serviceModel/bindings");ConfigurationManager.RefreshSection("system.serviceModel/client");ConfigurationManager.RefreshSection("system.serviceModel/services");

 

另外,值得一提的是:如果用IO操作修改修改app.config配置,直接使用相对路径"myapp.exe.config"来修改不可靠的,很容易出现找不到配置文件的异常(原因有很多种),需要使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性来获取配置文件的完整路径。

 

 

 

 

 

假设XXX.exe.config内容如下

 

以下方法,修改 endpoint 下指定根据 bindingConfiguration 的值修改 address

public void ChangeEndpointAddress(string endpointBindingConfiguration, string address){    var config = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;    XElement root = XElement.Load(config);    var quary = from ele in root.Element("system.serviceModel").Element("client").Elements("endpoint") where ele.Attribute("bindingConfiguration").Value == endpointBindingConfiguration select ele;    quary.ElementAt(0).SetAttributeValue("address", address);    //保存上面的修改        root.Save(config);    ConfigurationManager.RefreshSection("system.serviceModel/client");}

 

如 ChangeEndpointAddress("ProcessServiceSoap", "http://192.168.111.222/Services/ProcessService.asmx")

将原来的address192.168.0.123修改为192.168.111.222

 

 

 

 

 

 

 

首先假设你的应用程序配置文件如下:

 

Ok,那么如何在运行时去修改name的值呢??

有很多童鞋会说可以使用Xml读取配置文件,然后xxx。。。。

当然这种方法肯定可以解决问题,有没有其他方法呢??

在这里我要介绍一种比较简单的方法,可能已经有人知道了,那就是使用ConfigurationManager类

ConfigurationManager 存在System.Configuration.dll 中。

代码如下:

 

public static void Main(){    Console.WriteLine(ConfigurationManager.AppSettings["name"]);    ChangeConfiguration();    Console.WriteLine(ConfigurationManager.AppSettings["name"]);    Console.ReadLine();}private static void ChangeConfiguration(){    //读取程序集的配置文件    string assemblyConfigFile = Assembly.GetEntryAssembly().Location;    Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);    //获取appSettings节点    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");       //删除name,然后添加新值    appSettings.Settings.Remove("name");    appSettings.Settings.Add("name", "new");    //保存配置文件    config.Save();}

代码很简单:首先读取配置文件,接着获取appSettings节点,然后修改,接着保存。 运行:结果如下:

 

 

可以看到输出的值是两个old.

为什么??

 

查找msdn文档可以发现微软出于性能考虑,对ConfigurationManager采用了缓存策略,所以如果要读取新的值,应该使用ConfigurationManager的RefreshSection来进行刷新,

ConfigurationManager . RefreshSection:

刷新命名节,这样在下次检索它时将从磁盘重新读取它。

于是将Main方法修改为:

 

Console.WriteLine(ConfigurationManager.AppSettings["name"]);ChangeConfiguration();ConfigurationManager.RefreshSection("appSettings"); Console.WriteLine(ConfigurationManager.AppSettings["name"]);

 

重新清理解决方案,重新运行:

 

可以看到,仍然是两个old。。。

为什么??  

难道值没有修改??,我们打开应用程序的配置文件,可以通过监视assemblyConfigFile获得路径

上面是xxx\bin\Debug\CAStudy.exe.,对应的配置文件就是CAStudy.exe.config

 

文件的内容如下:

 

 

可以发现value 值已经更改,那么为什么输出还是old,old 呢??

 

 

为了验证不是VS2010的问题。

首先手动将CAStudy.exe.config 文件中的value改为”old”,接着再次运行CAStudy.exe 结果如下:

 

可以看到输出时old,和new。为什么会这样???

 

难道调试时读取的不是修改的配置文件,或者修改的配置文件并不是调试的应用程序读取的文件??

在assemblyConfigFile 中设置断点,可以发现assemblyConfigFile 读取的是CAStudy.exe.Config。但是vs调试的时候运行的是CAStudy.vshost.exe。也就是说我们使用ConfigurationManager.OpenExeConfiguration 打开的是CAStudy.exe.config文件,但是我们调试的应用程序CAStudy.vshost.exe使用的是CAStudy.vshost.exe.config文件。

那么还有其他的方式可以准确的获取应用程序配置文件吗??

有的,使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

将ChangeConfiguration()方法修改如下:

public static void Main(){    Console.WriteLine(ConfigurationManager.AppSettings["name"]);    ChangeConfiguration();    Console.WriteLine(ConfigurationManager.AppSettings["name"]);    Console.ReadLine();}private static void ChangeConfiguration(){    //读取程序集的配置文件    string assemblyConfigFile = Assembly.GetEntryAssembly().Location;    Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);    //获取appSettings节点    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");       //删除name,然后添加新值    appSettings.Settings.Remove("name");    appSettings.Settings.Add("name", "new");    //保存配置文件    config.Save();}

清理,重新运行:

使用默认的不传递字符串的版本就可以打开当前配置文件了。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

如果要查看当前配置文件的完整路径可以使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

重新运行,结果如下:

 

 

 

参考

https://blogs.msdn.microsoft.com/youssefm/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf/

http://developer.51cto.com/art/200908/146303.htm

http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET

 

转载于:https://www.cnblogs.com/code1992/p/11353053.html

你可能感兴趣的文章
Linux crontab 定时任务详解
查看>>
string成员函数
查看>>
onSaveInstanceState()方法问题
查看>>
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>
大数据时代侦查机制有哪些改变
查看>>
雷林鹏分享:jQuery EasyUI 菜单与按钮 - 创建链接按钮
查看>>
Apache Traffic Server服务搭建
查看>>
poj1990两个树状数组
查看>>
学习python-day1
查看>>
Zend_Db_Table->insert ()和zend_db_adapter::insert方法返回值不同
查看>>
递归问题
查看>>
Hyperledger下子项目
查看>>
Linq-查询上一条下一条
查看>>
常见前端开发的题目,可能对你有用
查看>>
BeautifulSoap库入门
查看>>
乐观锁与悲观锁
查看>>
Codeforces Round #328 (Div. 2)D. Super M 虚树直径
查看>>
Java判断是否为移动端
查看>>
chromedriver下载链接以及对应版本
查看>>
[SimplePlayer] 6. 音频同步
查看>>