App.config doesn't 'refresh' once runned?

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

Hi,

I have a Windows Service running (made in VB.NET), and wanted to be able to
change from time to time some parameters by changing them in the App.config.
But it seems that the application doesn't use the changed values in the
App.config, but continue to use the values that were there during start-up.

Is there a way to let the application use the new values in the App.config?
Is there kind of some 'refresh' function that I should run? Or should I do
this on an other way?

Thanks,

Pieter
 
I might be wrong but IIRC the app.config is part of the compile process, so I
think it's likely that you would have to recompile the project, unless you
specifically set it to look in that file during runtime (or know that it's
supposed to).
 
During (or after) compilation, the app.config file is just copied to the
target directory and renamed to <your_app_name>.exe.config. It's then read
on application startup.

I'm not aware of any way of reloading the configuration. The file contains
information that's required for application startup (like runtime
dependencies, etc.) which could not be reloaded.

If you need to change parameters during runtime, I suggest you either use
the registry or an some SQL database for your parameters OR use another
configuration file (say app.xml) and a FileSystemWathcher etc., but I guess
that would be way too complicated.

HTH,
Stefan
 
To answer your question specifically Dragu -

App.Config or exename.config changes are not read at runtime. (as far as I
know, and I think I'm right).

But it's easy to implement that. You need to implement your own app.config
basically, what I usually do is, I create a config directory. in that config
directory, I use a combination of FileSystemWatcher object and
System.EnterpriseServices caching to store config info that automatically
updates itself. The config info is nothing but an XML file, which is a class
that I serialized as XMLSerializer. So it's a very elegant solution.

An easier method is to put your relevant data in appSettings, and use the
FileSystemWatcher to re-read the appSettings whenever the file changes.

.... 4:30 AM .. EEEEEEEEEeeeeeeeeeeeee I need some sleep !!!

- Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik
 
Sahil,

Nice idea,

I do not mean to go to bed what is as well a good idea of course, however
the use of the filesystemwatcher for this.

Cor
 
The app.config is read once at startup. In order for your service to detect
any changes, without making any code changes, you would need to restart the
service (assuming you are chaging the app.config file directly in the
services runtime directory).

IIS will detect changes to the web.config by starting up a new app domain,
loading the modified web.config, and then servicing all new requests through
the new app domain, until the old one can be unloaded. This is your third
option, in addition to what Sahils suggestion and restarting the service.

Hope this helps
Dan
 
Pieter,
Just remember if you use ServiceController to stop & restart the service as
Dan suggests, that if you have multiple services in a single executable,
that you need to stop all the services for it to actually stop your EXE &
restart it.

In the service I am writing I plan on having the service manager (the
windows forms app) stop the service & restart my service (the windows
service app) via ServiceController.

If I had multiple services in a single EXE or just multiple services, I
would consider creating a second service that uses filesystemwatcher to
watch the app.config's of other services, then use ServiceController.Stop &
Restart to restart the respective services. Just be careful of the watcher
trying to restart itself ;-)

FWIW: I like the idea of separate App Domains, however what I am doing it
seems overkill right now...

Hope this helps
Jay
 
I have a solution which modifies app.config during runtime if that is
what you are getting at.

Post if you are interested.
 
Hi scorpion,

I don't need it right now, but it will be really helpfull for future
projects, so it would be nice if you could post it here.

Thanks a lot in advance!

Pieter
 
The other posts seem a bit confusing. So here's a clearer reply
I have a Windows Service running (made in VB.NET), and wanted to be
able to change from time to time some parameters by changing them in
the App.config. But it seems that the application doesn't use the
changed values in the App.config, but continue to use the values that
were there during start-up.

The application configuration file is named after the process, so if you
have a process called app.exe the configuration file is called
app.exe.config. VS.NET allows you to add a configuration file to a project
and it calls it app.config. When you build the project it copies this file
to the output folder and renames it according to the name of the process.
Is there a way to let the application use the new values in the
App.config? Is there kind of some 'refresh' function that I should
run? Or should I do this on an other way?

You cannot do it. The settings in the config file should be treated like
command line switches, once the process has started you cannot change them.

Actually, that is not strictly true, but it is a good rule of thumb. Here's
how it works. Each application domain in the process will read the
configuration and get its own copy, normally you will have just one
application domain in your process. When the appdomain wants to read a value
the *section* that contains the value is read (the sections are defined in
the machine.config file). The system will get the combination of the section
from machine.config and the application's file and use this XML to
initialize a configuration handler. The handler will generate some kind of
object from this (usually a collection object, but it doesn't have to be)
and the system stores it in a Hashtable in the appdomain. The next time that
a value is read from the section, the object cached in the Hashtable is
used.

This means that for an application domain, a setting is only read once from
the config file and after that the cached value is used. If you want to
change the config file during runtime you will have to use something like a
FileSystemWatcher on the config file, and when the file changes you will
have to use the XML classes to read the file and update your settings.

Richard
 
Back
Top