Reputation: 995
I created a Windows service used to execute automated procedures (.Net code) for multiple periodic operations, like backups, sanity checks, reports generation, etc. After building the project, I installed the service with installutil
. Everything worked great.
I decided to move various "static" parameters for these automated procedures in the App.config
file. I uninstalled the previous version of the service with installutil /u
and built the new version of the project. In my build output folder, there's a AppName.exe
file and a AppName.exe.config
file, as I would expect. I installed the new version of the service, again with installutil
from the VS 2012 Developer Command Prompt as an administrator.
The problem is that the service doesn't seem to be able to read the configuration file from the ConfigurationManager
. The call for ConfigurationManager.AppSettings("paramname")
doesn't fail, but the resulting parameter value is an empty string. As far as I know, the problem occurs for all parameters and not only for specific ones. The parameters are located in the <appSettings>
section, under <configuration>
, like I've done multiple times before in various projects.
I don't know if it can help, but my service runs on the LocalSystem
account and starts automatically after installation and with Windows.
What did I do wrong?
Edit: I already tried uninstalling/re-installing the service (multiple times), like some stackoverflow answers suggests. Also, I'm not looking to update/refresh the file at runtime.
Upvotes: 2
Views: 8520
Reputation: 995
Turns out I was getting the configuration value in a variable declared with the same name as the property I was using to store the value. For an unknown reason, I had no warning from the compiler. Since a Windows service cannot be debugged, the solution was to review the code/rewrite this part from scratch, and that's when I saw the error.
Upvotes: 0
Reputation: 69
I solved this by setting the location of the config file in runtime. This enables me to place and name the config file where ever I want. I fetch the executing assembly path and then checks if the config file is there. Check the answer on this thread how to dynamically set the config file
This is how I fetch the executing assembly:
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var configFilePath = Path.Combine(path, "service.config");
Hope this helps!
Upvotes: 2