What would be the best way to save user preferences for a Windows service?

I am developing an application that will be implemented as a Windows Service , and I was wondering what would be the best way to deal with various settings (at the user and application level). The fact is that I'm not yet completely familiar with all the available options, so in principle, I prefer .NET to own System.Configuration (ConfigurationManager.RefreshSection ("appSettings") seems tempting), although I still can’t wrap my head around the whole picture , namely, where the app.config file for this service is stored, and so on.

So my question is for you guys, what would be the best way to save user editable configuration data for this Windows service? Thanks everyone for the feedback.

+4
source share
2 answers

Hmmm ... 'user-editable' configuration options for a Windows service ...

It should be borne in mind that the Windows service is running in the background, so it does not have a direct way for users to interact with it. What I did to get around this was to create a separate external application that interacts with Windows Service using WCF. Thus, user-configurable settings are saved as part of the external interface settings, not the Windows service. Settings are simply passed to the Windows service using a series of WCF messages when the user changes them.

In my case, I even added NotifyIcon to my external application and added logic so that the application could be removed from the taskbar while minimizing it. It works just like the task manager when you turn on the Hide When Minimized option. This gives the user the illusion of interacting directly with the service, although these are two completely independent processes.

EDIT:

In response to your comment, WCF is just a message API. Messages are usually defined as classes decorated with DataContract and DataMember attributes. The ServiceContract and OperationContract attributes define the WCF service interface. Once defined, creating and hosting a WCF service in your Windows service is easy. And if you have Visual Studio 2008, creating a proxy server on the client side is quick because VS2008 can automate it for you.

As soon as all this is done, your external application will simply create an instance of the proxy server on the client side and call the methods of this proxy. As each method is invoked, WCF serializes and sends the message to the WCF service so that it works. It then serializes any response, including exceptions, back to the proxy. From the point of view of the client side, for example, your external application, you simply called the function. This is the beauty of WCF! This is very similar to socket programming, except that you do not need to manage connections. WCF takes care of everything that concerns you.

Of course, all this suggests that you can at least use .NET 3.0. If you are using Visual Studio 2008, you are in good shape. Here are some guides to get you started:

Once you have the basic concepts, I would recommend looking at the Juval Lowy website . There are many free WCF-related downloads that I find very useful, albeit a bit more advanced. Become familiar with WCF concepts first before looking too far.

Again, the whole point is to help your users configure various aspects of your Windows service. If you do not provide a GUI for this, I’m not sure how they can handle it manually by manipulating the app.config file itself.

Hope this helps.

+4
source

If you just need a single name / value dictionary to store configuration parameters, then app.config is the easiest answer. In your solution, it is called "app.config", but when it is built it gets renamed the name of the executable + ".config".

+3
source

All Articles