c#


RefreshSection not working as expected


I'm trying to update the app.config file for a running application. The update does work (that is, the file gets updated), but when I re-read the file, it shows the old value. The answer to this question does imply that the app.config is cached, but that calling RefreshSection should force a re-read.
Here's the app.config (straight from an MS example):
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/ServiceModelSamples/Service/CalculatorService"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
contract="ServiceReference1.ICalculator" name="WSHttpBinding_ICalculator">
<identity>
<userPrincipalName value="migree#redmond.corp.microsoft.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
And here's the code for the console app that I'm using to update it:
static void Main(string[] args)
{
Console.WriteLine("Before change");
ShowConfig();
Console.WriteLine("Change");
ChangeConfig();
Console.WriteLine("After change");
ShowConfig();
Console.ReadLine();
}
private static void ChangeConfig()
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string appConfig = File.ReadAllText(configuration.FilePath);
appConfig = appConfig.Replace("localhost:8000", "myAddress.this.com:8080");
File.WriteAllText(configuration.FilePath, appConfig);
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("endpoint");
ConfigurationManager.RefreshSection("client");
ConfigurationManager.RefreshSection("system.serviceModel");
ConfigurationManager.RefreshSection("configuration");
}
private static void ShowConfig()
{
ClientSection clientSection =
ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
ChannelEndpointElementCollection endpointCollection =
clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
Console.WriteLine(endpointElement.Address);
}
}
The file does get updated, because I can see that in a text editor while the program is running... but the console shows the same values read twice. Most of what I've seen for RefreshSection seems to imply it relates more to appSettings, although I haven't seen anything that states that outright. Is it possible to cause a refresh on the app.config as I am trying to?
You need to add:
ConfigurationManager.RefreshSection("system.serviceModel/client");
Your calls to RefreshSection need to reference everything that inherits from System.Configuration.ConfigurationSection.
This solution replaces writing the file yourself and uses the Configuration object instead to make the changes in the specified section:
private static void ChangeConfig()
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var section = configuration.GetSection("system.serviceModel/client");
if(section != null)
{
var xmlString = section.SectionInformation.GetRawXml();
xmlString = xmlString.Replace("localhost:8000", "myAddress.this.com:8080");
section.SectionInformation.SetRawXml(xmlString);
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(section.SectionInformation.SectionName);
}
}

Related Links

How can I get values of a control from the user-defined control in the parent form
How to find and show multiple items from a List using a single item?
Why Dates appearing differently when opened in .CSV file
I have a bunch of types that afaic should be structs, but the performance is way lower. What am I doing wrong?
Nancy and TinyIoC causing Stackoverflow
MySql server for Visual Studio
Populate listview multiple times using C# Class
Binding a BitmapImage obtained from a ByteStream to an ImageBrush on UWP C#
LIKE clause not returning the values as expected
How to know client URL name?
Connect to bluetooth low energy from desktop application c#
How to schedule method calls in a webservice built with ASP.NET
2 Foreign Keys, 1 Navigation Property to pull through both
directx.matrix.SetTransform() seems not works in the windows message callback function
MVC5 - Model is empty after POST
How to get facebook likes count only for a facebook page?

Categories

HOME
asterisk
postgresql
leaflet
ssh
hibernate-mapping
clip-path
box-api
adroitlogic
http-status-codes
hql
infopath
tag-cloud
css-float
reportlab
aiml
accordion
summernote
lotusscript
inline-assembly
rancher
akavache
expressionengine
smart-device
checkmarx
hierarchy
openoffice-impress
node-soap
jsonapi-resources
ilog
fastadapter
http-proxy
lfe
ng-lightning
wicket-tester
file-descriptor
collation
bootstrap-carousel
togglebutton
x12
disconnect
geokit
diameter-protocol
supervised-learning
database-administration
ewam
matlab-engine
android-contentresolver
play-json
properties-file
litedb
euro
opennebula
nexusdb
douglas-peucker
famous-engine
reactjs-native
pidgin
svg-android
ardor3d
misfire-instruction
sygic
octokit
codeigniter-form-helper
pudb
qt5.1
netbiscuits
netzke
beep
mysql-connector
nsundomanager
yii-cactiverecord
umfpack
clgeocoder
mcpd
mismatch
symfony-1.2
jquery-attributes
data-formats
cons

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile