2

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.

Benjamin Beaulieu
  • 995
  • 2
  • 10
  • 26
  • 1
    Depending on how you coded your service you may need to restart it in order for it to pick up any changes in the app.config. For example, if it reads the config sections during it's OnStart and stores them in a variable then it won't pick up changes unless you restart the service. So the question is: where do you make the call to `ConfigurationManager`? I've even seen cases where you have multiple services in one exe and it doesn't necessarily pick up the changes until all of those services are restarted. – NotMe Oct 01 '13 at 14:43
  • I'm calling `ConfigurationManager` once in the constructor of each of my automated procedures (which are called during OnStart), but I'm not doing any changes at runtime, so I don't think it should be a problem. Also, I only have one service and one installer in my project. – Benjamin Beaulieu Oct 01 '13 at 14:52
  • 1
    It's interesting that it doesn't fail but doesn't get the value. Can you post the actual code that is loading the app setting and a copy of the config file? – NotMe Oct 01 '13 at 14:59
  • Due to confidentiality issues, I cannot post the actual code. However, I'm simply using `ConfigurationManager.AppSettings("paramname")` with a matching key/value `` in the `` section. Could the fact that I used VS 2012 Express without the Windows Service template be the cause? I started from an Empty Project. – Benjamin Beaulieu Oct 01 '13 at 15:18
  • 1
    That's unrelated. if you were able to create and install a service, then you have a service. Without actual code I can't help. All I can say is that it's likely the service isn't reading the config file you think it is OR that the code itself is failing (wrongly named parameter) and you have some sort of try .. catch doing a silent fail. Essentially basic code review stuff. Good luck. – NotMe Oct 01 '13 at 15:20
  • Thank you for your help. I'll try waiting a bit more for an answer and move on to another solution if I can't work my way through. – Benjamin Beaulieu Oct 01 '13 at 15:24

2 Answers2

2

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!

Community
  • 1
  • 1
FredrikR
  • 69
  • 3
0

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.

Benjamin Beaulieu
  • 995
  • 2
  • 10
  • 26
  • 2
    For debugging a Windows service see: http://www.codeproject.com/Articles/10153/Debugging-Windows-Services-under-Visual-Studio-NET and the discussion below. – Bernhard Hiller Oct 09 '13 at 13:02