Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Windows Service that needs a significant amount of user input. Yes, I can put those values in the app.config file but then that seems lazy. This UI includes the ability to configure the service, start/stop the service and a log viewer. So, I'd like the Windows Service to pick up the values entered by the UI. Does anyone have a "standard" or "best practices" way of doing this? Or, even just a better idea than my own.

My current approach:
The UI's data context is an object that contains all of my UI entries. Serialize the object as XML using XmlSerializer. Then whenever the Windows Service cycles, go to this XML file and deserialize it. Then, use that object in the Windows Service.

>My question on this approach is... what is the best way to tell the Windows Service where the UI was installed? I don't want to hard code this because the installer could put it it on the D:\ or who knows where? Once again, I think making the user enter this value in the app.config is a lazy approach on the developer front.

Ideas:
I attempted to use the Process of the UI to find the Environment.CurrentDirectory of the UI process but that doesn't seem to exist. Remember, the UI starts/stops the service so I know the UI will be opened when the service first starts. Anyone have ideas on this front?

When the UI is installed or starts the first time, I could save the directory to a registry setting... but that seems pretty old school. Is there a better way to do this?


Any suggestions are appreciated.

Thanks,
Joel
Posted
Updated 16-Jun-11 5:23am
v2
Comments
Kim Togo 16-Jun-11 14:14pm    
Please use "Add Comment" under the solution :-)

I have developed some approach related to it.

First of all, Windows Service cannot have UI. It should keep working when a user logs off and when nobody has logged in. It does not mean that the same assembly cannot be used it two modes: interactive and Windows Service.

I've created WPF components to be usable from a library (this is some problem by itself, XAML application can not be used). A UI application can start of not, depending on the mode of operation. If the same application is started under Service Controller, the UI is not started.

The difference between two modes of operations is easily detected by the static property System.Environment.UserInteractive. (!)

Now, the UI mode is used for installing the service, for starting/stopping it; and you can use it for communication with the another instance of the application run as a Windows Service. Best way to communicate is probably WCF self-hosted by the Service with the channel based on named pipes (IPC), because both instances work on the same computer. Of course you can make them interacting via network as well.

More than that, I implemented the option to run the core of the service in the interactive mode. Unfortunately, there is no interface if the service which encapsulates the Service methods OnContinue, OnPause, OnPowerEvent, OnSessionChange, OnShutdown, OnStart, OnStop and ServiceFeatures Features. I simply created such interface. The user of the library is supposed to implement this interface instead of implementing the Service itself.

The library hosting my dual-mode service runs the implementation of this interface in one of two modes. If the mode is Windows Service, the implementation of the interface is used to implement the correspondent methods of the Service. If the mode is interactive, the method runs inside the simulated Service environment modeling all those Start, Stop, etc. One point of interest: the host provides API for logging. In the case of Windows Service mode, the logging uses System.EventLog and log data goes to the system log. In case of interactive mode, logging goes to the Window component (a list box) and is visualized immediately.

Implementation of industrial-grade application controlling hardware in production had proven effectiveness of this approach during debugging of the Service: most of the debugging was done in interactive mode which is of course much more convenient.

—SA
 
Share this answer
 
Comments
Kim Togo 16-Jun-11 14:13pm    
[From OP]
Thanks for the response SA. I'll think do some research on your approach.

What I maybe didn't make as clear as I should have in my initial writeup is that I am not trying to tie the Servce and the UI together for direct messaging. I'm simply trying to take the output from the UI and save it off. Then, the service needs to use the data.

What I haven't tried is a self hosted WCF service. This sounds like a good approach. However, I can't assume that I will always have IIS installed on the machine. If you have a link or something that gets me started on understanding this approach, I'd appreciate it.
Sergey Alexandrovich Kryukov 16-Jun-11 14:31pm    
Wait, wait... Self-hosted WCF means that you don't need anything except .NET with its standard libraries. Nothing else. This is pretty much like classical remoting, simplified; for basic usage it's easier to get started, for a tricky cases classical remoting (which is just be on the low-level side) could be better (many say it's obsolete, but I disagree; and officially it is not obsolete).

Now, you don't have to have dual application I described; you can have two different applications (Windows Service and UI for Web Service control); they would simply share some of the referenced assemblies. I described the benefits of my approach (first of all, debugging), but it certainly the more investment in code base.

So, are you going to formally accept this solution (green button)?
Thank you.

Feel free to ask you follow-up questions. I just could not cover more in one post before having any feedback.
--SA
thatraja 17-Jun-11 11:40am    
Simple Suggestion SA.
I think you should have replied this to OP(I know that Kim brought OP's comment to your answer) below his question or you could update your comment in your answer & simply added a comment like "Check my updated answer".
Because most OPs never find these things. They are looking at only answers.
Sergey Alexandrovich Kryukov 18-Jun-11 2:12am    
Thank you for the note.
First of all, misplaced level of comment happen all the time.
This is not a problem as far as I can see. OP gets this comment anyway.
Finally, I sometime use deeper level of comment to notify two or more people at once. This time I had no choice as there is not OP's comment. I hope Kim won't find my reply too annoying.
--SA

P.S.: few times I have a long conversation with other expert under the comment of OP -- comparison of our native cultures, humor, funny stories, philosophy of programming, plans for writing articles, a car accident... Poor OP could have been annoyed very much -- to our sins. :-)
thatraja 18-Jun-11 2:20am    
/*I hope Kim won't find my reply too annoying.*/
No I don't mean that(Authorities know that you are replying for their comment[OP's]). The thing is OP couldn't get the notification for your reply.
I'm always doing my way(which is in my suggestion) & also left a note for authorities(like Kim here)....It worked sometimes for me. So I have suggested you.
A service can't have a UI. You have to do like SA said - create a separate app (that lives in the system tray) that can communicate with and control the service.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 14:47pm    
Sure, a System Tray is the best location for such UI. Good point, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900