views:

113

answers:

2

I'm developing an application that's going to be implemented as a Windows Service and I was wondering what'd be the best way to deal with the different settings (at User and Application level). The thing is, I'm not completely familiar (yet) with all the available options, so in principle I'm favoring .NET's own System.Configuration (ConfigurationManager.RefreshSection("appSettings") seems tempting), although I'm still failing to wrap my head around the whole picture, namely, where is the app.config file being stored for a given service, so on.

So my question for you guys is, what would be the best way to store user-editable configuration details for a given Windows Service? Thanks everyone in advance for the feedback.

+1  A: 

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

David
Thanks Dave, mind elaborating where is it exaclty located right after I do the **InstallUtil.exe /i MyWindowsService.exe** thing?
Nano Taboada
I assume you have an app.config file as part of your solution. When you build your solution, it gets copied to the output directory (normally named 'Debug' or 'Release) along with your executable and renamed to the name of your executable plus the .config extension. For example, if you executable is MyWindowsService.exe, the app.config file will be renamed to MyWindowsService.exe.config. But even then, there is not a convenient way for your users to edit this file once it's delivered. That's why a front-end app makes a lot of sense here.
Matt Davis
Also, this config file gets overwritten every time you install a newer version, so administrative changes to this file are lost each time. That sucks, but storing the settings in a user.settings file is counter-intuitive for anyone unfamiliar with the inner-workings. I haven't found a good solution yet.
uosɐſ
+1  A: 

Hmmm...'user-editable' configuration settings for a Windows Service...

The thing to keep in mind is that the Windows Service runs in the background, so it does not have a direct way for users to interact with it. What I've done to get around this is to create a separate front-end application that communicates with the Windows Service using WCF. In this way, the 'user-editable' configuration settings are persisted as part of the front-end application's settings, not the Windows Service. The settings are simply communicated to the Windows Service using a series of WCF messages as the user changes them.

In my case, I've even added a NotifyIcon to my front-end application and added logic so that the app can be removed from the taskbar when it is minimized. It works the same way Task Manager does when you turn on the 'Hide When Minimized' option. This gives the user the illusion of interacting with the service directly, even though it is two completely independent processes.

EDIT:

In response to your comment, WCF is simply a messaging API. Messages are usually defined as classes decorated with the DataContract and DataMember attributes. The ServiceContract and OperationContract attributes define the WCF service interface. Once these are defined, creating and hosting the WCF service inside your Windows service is easy. And if you have Visual Studio 2008, creating the client-side proxy is a snap as VS2008 can automate it for you.

Once all this is done, your front-end app simply instantiates an instance of the client-side proxy and invokes the methods on that proxy. As each method is invoked, the WCF framework takes care of serializing and sending the message to the WCF service for it to act on. It then serializes any response, including exceptions, back to the proxy. From the perspective of the client-side, e.g., your front-end app, you've simply invoked a function. That's the beauty of WCF! It's very analogous to socket programming, except you don't have to manage the connections. WCF takes care of all that plumbing for you.

Of course, all this assumes you can at least use .NET 3.0. If you are using Visual Studio 2008, you're in good shape. Here are a couple of tutorials to help you get started:

Once you have the basic concepts down, I would recommend looking at Juval Lowy's website. There are a lot of free WCF-related downloads there that I find very helpful to look at, although it's a bit more advanced. Understand the WCF concepts first before delving too far there.

Again, the whole point of this is to help your users configure various aspects of your Windows Service. If you do not provide a front-end GUI to do this, I'm not sure how they'd do it short of manually manipulating the app.config file itself.

Hope this helps.

Matt Davis
Matt, thanks much for your comment, really appreciate them. Your approach seems interesting although since I'm not familiar with Communications Foundation yet I'm concerned about how step that learning curve might result. Would love to read your thoughts about that.
Nano Taboada