views:

8434

answers:

8

Hi,

I've developed a windows service application using Visual Studio 2008 / C#.

I have an app.config file in the project. When installed, the app.exe.config file appears beside the executable but it appears not to be reading the values from it when I try to access them through ConfigurationManager.AppSettings.

Has it copied the config file elsewhere or is there some other problem I don't know about?

Thanks in advance,

Martin.

Edit: The config file name is infact my_exe_file_name.exe.config, it looks like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>

    <add key="RuntimeFrequency" value="3" />

  </appSettings>
</configuration>

and I am trying to read via:

ConfigurationManager.AppSettings["RuntimeFrequency"] The debug value I continually see is '1' and not '3'. Am I doing something wrong here?

A: 

App.config file should be renamed to your_exe_file_name.exe.config and placed near the exe file.

Alex Reitbort
A: 

To ask the obvious, is the file still called "app.config"? as I'd expect it to be "Yourservice.exe.config"

Rowland Shaw
A: 

The config file name is infact my_exe_file_name.exe.config, it looks like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>

    <add key="RuntimeFrequency" value="3" />

  </appSettings>
</configuration>

and I am trying to read via:

ConfigurationManager.AppSettings["RuntimeFrequency"]

The debug value I continually see is '1' and not '3'. Am I doing something wrong here?

Dude, don't add detail to your question in an "answer", you can edit your own questions. Please read the faq (link on top of page, center right)
Binary Worrier
+1  A: 

Maybe you are updating the wrong config file. You should double check that using

System.Configuration.ConfigurationManager.OpenExeConfiguration(PATH_TO_CONFIG);
Igor Zelaya
A: 

Is it possible that you have more than one instance of the RuntimeFrequency entry defined? The ConfigurationManager reads the file from top to bottom and processes each setting individually. Therefore, the last value of RuntimeFrequency that is defined in the file is the one it will use.

If you want to know for sure if your file is being used, I would simply remove or comment out any definition for RuntimeFrequency (copy/paste errors do happen) and wait to see an application error when ConfigurationManager attempts to reference an entry in AppSettings that does not exist.

EnocNRoll
A: 

Generally for the Windows Services that I write, i drop the appName.exe.config file into C:\WINDOWS\system32\

Perhaps you have an older version in that directory, which is where your service is getting the value, even though you've updated the config file in your project.

Dave
Well, svchost.exe runs the windows service. And svchost.exe exists in the %WINDIR%\System32 directory. Hence, that directory will be searched first for the myapplication.exe.config file. Correct me if i'm wrong.
Dave
A: 

When you are in debug mode check and see what settings are in the my_exe_file_name.vshost.exe.config Also make sure you adjust this in the app.config file. Visual studio should update the final config file in your bin/debug folders.

Aaron Fischer
+6  A: 

I located the error and it was related to file permissions. After installing the service, my local user account didn't have access to modify the app.exe.config file.

The tool I was using to edit was not informing me it was being denied access to save the file - that's notepad++ if anyone is interested - so I couldn't see that it wasn't saving over the old config file.

Solved now, thanks everyone.

Martin.

I guess you should "Accept" your own solution. Cheers...
EnocNRoll