Windows Service/app.config

T

TomB

I've written a simple service that reads the app.config file on start.

It works fine if I start it from the VS IDE -- F5 then installutil

I then created a package, wherein the content of app.config has "" for the
values. With the idea that my user, can enter the information before
starting the service. However; this doesn't work. I'm very confused.

As a test, I "emptied" the app.config file and rebuilt, then did an
installutil. This recreated the error, however; if I did an installutil /u
changed the app.config file (in the bin) and re-installutil-ed it - it
worked.
I then tried installing the app using the package I created. I then went to
that directory and tried the above (uninstall, edit, reinstall) without
luck.


My purpose in using the app.config file, is to point the service to the
location of a different configuration file (as I need the service to monitor
that file)

I'm quite sure it isn't a problem with my code, but would be happy to post
it if necessary.

Am I right that the app.config file is read once, when the service starts?
Or just when it's installed (which seems pointless)

I've been hunting around google and msdn, but not having any luck.

Thanks
TomB
 
J

John Vottero

TomB said:
I've written a simple service that reads the app.config file on start.

It works fine if I start it from the VS IDE -- F5 then installutil

I then created a package, wherein the content of app.config has "" for the
values. With the idea that my user, can enter the information before
starting the service. However; this doesn't work. I'm very confused.

As a test, I "emptied" the app.config file and rebuilt, then did an
installutil. This recreated the error, however; if I did an installutil /u
changed the app.config file (in the bin) and re-installutil-ed it - it
worked.
I then tried installing the app using the package I created. I then went to
that directory and tried the above (uninstall, edit, reinstall) without
luck.


My purpose in using the app.config file, is to point the service to the
location of a different configuration file (as I need the service to monitor
that file)

I'm quite sure it isn't a problem with my code, but would be happy to post
it if necessary.

Am I right that the app.config file is read once, when the service starts?
Or just when it's installed (which seems pointless)

I've been hunting around google and msdn, but not having any luck.

Yes, the config file is read at run time, the first time it's accessed, not
when it's installed. Beyond that, I can't help much because it's not clear
what your error is, you say "it doesn't work" but you didn't tell us what
you mean by that. Is an exception being thrown? Are you getting back
unexpected values?
 
T

Tom B

Thanks for the reply, I guess I wasn't clear.

The service doesn't start, and errors out, because the value of my string
(read from app.config) is empty.

I don't have the source code with me, but my string(a file location) is read
from the app.config. I know it's not getting populated because my Error Log
(where I sent any errors) shows FileNotFoundException.

Thanks
TomB
 
J

John Vottero

Tom B said:
Thanks for the reply, I guess I wasn't clear.

The service doesn't start, and errors out, because the value of my string
(read from app.config) is empty.

I don't have the source code with me, but my string(a file location) is read
from the app.config. I know it's not getting populated because my Error Log
(where I sent any errors) shows FileNotFoundException.

Does the config file contain a complete file specification? It could be
that the file isn't found because services start with a default directory
that's in the system root, not the directory where your executable is
installed.
 
D

Dale Roberts

This is just a reminder that when you build a service with .Net, the
App.config file gets copied into an applicationname.exe.config file.
Rebuild in development mode automatically updates this copy, so it
appears like it's reading App.config directly, but it is not. Instead,
look for a *.config file in the bin directory. If you use
System.Configuration to read the AppSettings in the OnStart event, this
is the file being read. You can change the file, stop the service,
start the service, and use the new configuration setting without
uninstalling and reinstalling.

I typically write to an event log during startup so I can verify that
the directory in the config file loaded properly.

Thanks,
Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top