user2143207
user2143207

Reputation: 69

ConfigurationManager.RefreshSection not working in c# windows service

I am having two projects one is basically a Windows service and second is class project in which I am doing my business processing. My App.config file is in Windows service project and in my Class project I am using below

ConfigurationManager.RefreshSection("appsettings");           
string scheduledTime = ConfigurationManager.AppSettings["ScheduleTime"];

this setting is in my appsettings section of config file

I am using RefreshSection and also updating my app.config value in windows service project but its not getting updated at run time in my class project.

What is the catch in this ?

Upvotes: 3

Views: 3584

Answers (2)

Alex KeySmith
Alex KeySmith

Reputation: 17101

I'm having a little trouble myself with something similar, however I did come across something for AppSettings. Give this a shot:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

I think appsettings should be appSettings

Upvotes: 3

Hamed Nikzad
Hamed Nikzad

Reputation: 668

If you want to get latest value use this code:

var appSettings = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location).AppSettings;
var mySetting = appSettings.Settings["keyOfSetting"].Value;

remember every time you need latest value, must use two line of code in same place!

Upvotes: 1

Related Questions