Tracking windows service status and displaying the status in WPF UI - c#

I am developing an windows application using WPF and this application follows MVVM. My application displays the status of two Windows services. That is, if the application is open and the service is stopped, immediately the status needs to be changed in UI. Similarly if the application is open and the service is started, immediately the status needs to be changed in Application UI.Before, I never needed to handle windows services. So please help me.

You can use the ServiceController class to poll for the service status at intervals.
With this anyway you can't really have an immediate notification, there is a poll delay error. If you want something more accurate you should modify the services to send its status to a listener using some IPC mechanism.

Related

C# Show windows service updates notification to user

I am new to .NET and seeking help for the Windows Service Updates Notifications.
I have a use case that is somewhat similar to "https://stackoverflow.com/questions/41232170/c-sharp-show-notification-prompting-a-update".
The application is developed in C#.NET and is deployed and running as Windows Service.
However, the application is not using any MSI installer to install it. I am using a batch script that configures the Windows Service application.
Now, I want to show the notifications about the updates about the Windows Service to the user, when the system gets restarted.
I came across about the usage of WCF or by using the Task Scheduler, but not sure which one would be the better solution.
Please advice.
Ok, there are (were, because MS disabled the first one that I'm going to explain) two ways to notify your user about updates from a service.
First, the bad, ugly (and non-working in recent versions) way: interactive services.
You can configure a service as interactive, if you add the SERVICE_INTERACTIVE_PROCESS flag the service will be able to create a GUI that will be attached to Display_0. This presents a ton of problems (trying to show a GUI when there's no user session, if you have two sessions open only the first one will show the GUI and so on) but it's a cheap dirty way to show data to the user. Avoid it.
Second, the right way: a standalone GUI program.
In this case you create a secondary program that will show the data to the user, you can start it with the user session or let the user decide if he wants to receive info by opening manually this application. This program needs to receive the updates from the service in some way, which is better is up to you but I would use UDP for this, in this way your service doesn't needs to care if any GUI app is connected or not, you broadcast an UDP message and everyone listening will receive it, you don't need to mantain a server that handles connections, you don't need to have an storage in order to maintain the event data and it will support any number of instances of the GUI (if more than one user has started a session in the machine all of them will get notified).
But as I said, that would be my preference, you can do it as fancy as you want, you can use Pipes, use a file that contains the event and use a FileSystemWatcher to get notified when changes happen in it, you can even host an ASP .net web app which implements a SignalR hub and then you can create your GUI in html. It's up to you decide which mechanism is the best for your scenario.

Communication between service and application And also to trigger the application with GUI

I have developed an application which does periodical jobs. It runs as windows service in background and manages jobs. I want to trigger another application with GUI from service application when any error notification needs to be given to user. So after seeing err message user will give some response. So now service application (running as service) should do some work based on user response in GUI application (with interface). User response should be communicated to the service application.
So my requirement is
Application running as service should be able to trigger another application with GUI (its sufficient if triggering happens only when users are logged in)
Communication between an application running as service and application with GUI.
I saw many links about communication between service and application. Named pipes requires .net 3.5 and above, but I want my application to work in .net 2 alone. Writing to a txt file and reading it is last option for me as it is not the best method.
So how it can be done? Please give me ideas to start with.

How can I develop an agent to notify user

I would like to develop a program (an agent) that will run on the user's desktop. It has to communicate with a web service periodically then if a state changes it will notify the user in a notification balloon.
Can I do a winforms or wpf project? (that will both communicate with the web service and notify the user)
Or should I do separately a windows service that will firstly communicate with the web service and then communicate with another standalone winforms app that will notify the user?
Thanks.
Periodically fetching status through service
You can implement an executable(Console) application which should have the logic to invoke the web service to fetch the status. You can use windows task scheduler to execute this exe periodically.
There are many task scheduler wrapper libraries are available, Use could use any of them based on your need.
Task Scheduler Managed Wrapper
Quartz Job Scheduler
Notifying user
If the status is changed then you can notify the user through any User interface, this can be WinForm or WPF application. When the exe which is initiated by the task scheduler finds a status change, it will execute the UI application that you have created to notify the user.
If you need only a single Form to notify the user then Windows Form should be enough unless you want to show any fancy UI using WPF.
Why Windows Service might not be a good choice
It's actually easy to use a windows service to periodically call the web service instead of using a Task scheduler, but the problem you will face with the windows service is that it can't easily communicate with user's desktop, that means it can't directly show/execute any User interface application on user desktop. You will end up writing a IPC(Inter Process Communication) mechanism to communicate with any UI application from windows service.

Best way to check if a service has hung

I have a question about how best to check if a service is still running.
First a bit of clarification. The service I have is a C# application which can either be run from the command line or can be run as a Windows Service. The function of the service is to check for changes to a remote 3rd party data source and process those changes before adding them to our own local data store.
I want to be able to identify when the service has stopped functioning for whatever reason, and notify somebody when this happens as automatically as possible. This needs to happen regardless of whether the service is being run as a Windows Service or from the command line.
I have already considered monitoring the local data store for changes and notifying when changes haven't happened for a set amount of time, however this has proven to be a little too inconsistent, because the frequency of changes to the 3rd party data source is variable, which means that a prolonged lack of changes doesn't necessarily indicate that the service has stopped working, it could just be that there are no changes!
Are there any suggestions about how I might go about monitoring this? Anyone got any experience working with something similar?
Thanks, M
Edit 1
Just to give a rough idea of how the service works: The 3rd party service raises events when new/updated data is available so my service sits and waits for these events to be raised and processes the data returned in the raised event. Therefore this is why it's tricky to identify when there's "no changes" rather than "service crashed".
Edit 2
I think I need to be a little clearer: The main reason for this monitoring is to notify a user about a potential issue either with the service or with the connection to the 3rd party service. The service itself is single threaded and has proper exception handling and logging. Chances are this service is going to be run on a server somewhere so if there are any problems with the service and it stops updating our local data store for whatever reason the service needs to notify someone.
you might want to consider something like a 'heartbeat':
Heartbeat activity for Windows Service
But your main consideration should be working out why your service should be able to stop/hang? All exceptions need to be caught, and at the very worst case, reset your service to its start state after a short wait to prevent CPU maxing.
Windows itself has a variety of methods to help also:
Start > Run > Services.msc > Right Click Service > Properties > Recovery Options
If you design your application to properly use exceptions and handle them appropriately, you shouldn't ever have a problem with your service 'hanging for some reason'.
Additional:
Is there no way for you do determine the difference between "no work required" and a hang?
you can use ServiceController class in .net to monitor services.
I faced the same problem in one of my projects.
I used the below approach to monitor the my service.
First thing, i logged all the informations,errors from my service to event viewer in a standard format like this
custom-eventid|datetime|message
Then i created one more notification service which will listen to the
event viewer for the particular events,reads the message from the
event entry
if the event entry falls in the event viewer it will send mail
notification through smtp
if you are not provided with the smpt then go for windows application
which listen to the events and shows message using baloon or message
box
The solution that worked for this project was to use a heartbeat like implementation to allow the service to notify me of it's availability.
Because our application uses WebAPI I was able to set up an endpoint which the service "pings" every [x] seconds.
A separate process has been added which checks the date and time of the last notification from the service and if that doesn't fall within a set threshold I notify the user that the service is unavailable.
I looked at using the ServiceController but that was not going to be an ideal solution because of the possibility that the functionality added to the service could be run as a Windows console application instead of a Windows Service.

Is there an .Net API to detect status changes of a windows service without polling?

Hi I am using the ServiceController class to start and stop a windows service, I can also query the Status property to detect whether the service is running or not. I'd like to monitor and display the status of this service in my application. Unfortunately this seems to be only possible by polling the Status property. I'd rather use some notification mechanism.
Is there any event in .NET which can notify me that the service status has changed?
If you are targeting Windows Vista or higher you can call NotifyServiceStatusChange
WMI will poll the services for you. Check How can I monitor status changes of windows services under windows xp?
Not that I know of, but you could look at using WMI
http://msdn.microsoft.com/en-us/library/bb404655.aspx
Unless they've added something in .NET 4.0, you'll have to poll. This is the approach that I am currently using my project. I created a class that allows interested parties to specify which service to monitor. A background thread is then started that monitors the service status once every second. If a change in status is detected, an event is fired to notify the interested parties of the old and new statuses.

Categories

Resources