views:

109

answers:

3

I am working on a .NET Windows Service where I am trying to store settings that will be used when the service is started and while it is running. I have searched through posts on SO and found that using the Settings in the properties of the project is great for use with console and winforms applications. However, Google and SO are silent when it pertains to storing these settings with a windows service.

Does anyone one know if it is proper to use these settings in a .NET service? If not, is Serialization my next best choice? Has anyone had practical uses for settings in a service and found that it is best to use a specific method?

+1  A: 

I use the Settings.settings stuff for storing the config for my services and I've had no problems. Just the usual that the user settings that are changed will be stored in it's usual obscure location that you have to hunt around for if you want to edit them by hand.

ho1
A: 

I dont see any reason not to use Settings in the properties of the project as you would for a winForms app. We do this, and it works fine.

barrylloyd
The problem with using Settings in the properties is that it depends on the logged user as the settings are stored in the user profile. If multiple administrators are using the service then the settings will change.
Amr
+2  A: 

I've had problems with using Settings.settings. For instance, if you need to make changes at run-time, there can be problems with the settings being overridden by those that were initially stored in the settings.settings file as opposed to what's shown should be stored per the app/web.config. Consequently I make all my Web Service proxy settings "static" in the properties and pull them manually from the app/web.config via a helper method and programmatically set them. This circumvents any problems.

An example of the issue we had: I pointed my development machine to a web service on a test server to test the code that consumed the web service. When the code was moved to our test server, no problems manifested - as the test server was still pointed at the same web service on the same test server. However, when we moved the application to the production server and reconfigured the web.config to point at the production server we started getting screwy results. It took quite a bit of effort to pinpoint that even though we had reconfigured the application to point at the production server's implementation of the web service, it was still connecting to the web service on the test server. It wasn't until we changed settings.settings on my development machine and recompiled the application that it worked. Further to this, we also noted that if there were DNS problems connecting to the production web service, rather than fail, it fell back to the original settings that were specified in the settings.settings from when we created the web service proxy in our application - the proxy generator actually hard codes them. Consequently when there were network outages, instead of getting easily diagnosed connection failures, it simply fell back to the test server and we started getting incomprehensible data issues. I'm not sure if this was a known problem or if it's been fixed, but it's certainly something you should be aware of.

Consequently, since then, I've always set the service properties to static and used a helper method to read the correct settings from the web.config directly and written them programmatically as this seems to circumvent the problem.

It may seem like the problem I had has nothing to do with yours because I was using Web Services which isn't anything to do with Windows Services, however, any environment where you need to be able to change the settings at runtime without having to recompile could be affected by this issue, so you should be aware that if you run in a Dev/Test/Production environment or indeed any environment where you need your app to be reconfigured at run-time (i.e. without having to recompile) that you can get unpredictable results when using settings.settings. Beware.

BenAlabaster
@BenAlabaster, Thank you for the very detailed answer. I would up vote it, but I do not have enough reputation points yet. Thanks for taking the time to answer this.
AndHeCodedIt
Ive got to admit I havent experienced these problems at all, and we use a lot of windows services with app settings read from the app.config file - including web service URLs.Do you think this problem is particular to windows services v win forms apps, or just a general app.config bug/issue?
barrylloyd
@barrylloyd - I didn't do a whole lot of investigation as to which areas of the framework this affected to be honest as it was a mission critical application [i.e. get it fixed and move on]. From the investigation I did, it seemed that the issue may have been regarding the interaction of the web service proxy with settings.settings rather than connecting properly to the web.config to look up its settings. It may be that it only affects this area of the framework, it may affect other areas, I'm not sure.
BenAlabaster
@barrylloyd @benAlabaster I think the issue is how you're accessing the settings. I think it's like this: If you access them using `My.Settings` in VB.Net it'll read the setting from the .config file, but if you're using what looks to be the same in C# (`Settings.Default`), then you'll read the default settings that are compiled in. To avoid this in C# you have to create an instance of the settings object I think. It's of course also important to remember to reload and save the settings when needed (I usually do that in the OnStart and OnStop).
ho1