Updating config file and updating the values in application

放肆的年华 提交于 2019-12-05 13:02:25
cRichter
var client = 
 System.ServiceModel.ChannelFactory<ISampleService>(
  System.ServiceModel.Channels.Binding binding, 
  System.ServiceModel.EndpointAddress remoteAddress)

you can connect to a service also programmatically, and give the WCF directly the config needed.

with using this, you do not need the wcf config in the exe any more.

https://msdn.microsoft.com/en-us/library/ms576132.aspx

Settings with scope "User" can be easily stored and retrieved while the Application is running. If your settings are of scope "Application" I'm afraid you cannot modify and reload them without restarting your application. You'll need to roll your own configuration solution then.

There are 2 parts to making this work. 1) Updating the correct config file, and 2) forcing .net to reload the changes.

1) When a .net process starts, it will copy the existing .config to the vshost.exe.config file. If you update the original config file after the process has started, you will not see it in the vshost.config until you restart the process. So to make this work at runtime, you need to update the vshost.exe.config, not the exe.config file.

2) To force .net to reload the settings, you need to tell the configuration manager that the settings changed. You can do this with the ConfigurationManager.RefreshSection().

There is some more information and a couple code examples at: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3c365fdb-563e-4b0a-a4b4-df684c2dd908/

Basically, Microsoft designed it this way (having the configuration read at start up and not again), specifically to discourage you from trying this, because the *.config file live in the C:\Program Files folder, and that should not be writable by a non-Administrator.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!