Windows services - c#

I have a windows application and I need to change it into a windows service, How can I check whether the windows service is running or not without using any external service to keep a check on the main service.
Is there any way to check it within the main service as I have tried work on it but if the windows service is stopped then there is no way I can check on it.
Do I have to implement the thread(keep alive thread) for this, if so then how to do it?

If your main service is not running, how can it check anything?
You have to have a second monitoring service of some kind.

Have a look at
How can I verify if a Windows
Service is running
Detect the Status of a Windows
Service

Related

Restart Windows service from another service

I am developing a Windows Service that will control a group of windows application and run them on scheduler given from webservice.
I am not able to Start/Stop the services due to security issues. I have tried this solution but it did not work.
Try use WMI for manupulation of windows service state. Read here

why does my service says it's running even though it's not?

I created a service using .NET that is supposed to start automatically and run as a User with my username & password.
If I restart my PC, log on with the same username & password, and go to Task Manager, it says that the service is running, but when I check the Event Log there are no messages stating that the service has started, and in fact, the service has not started, because it doesn't do what it's supposed to be doing.
On the other hand, if I manually stop and start the service, then the entries in the Event Log appear and the service runs as it's supposed to.
So when I restart my PC, how come Task Manager says that the service is running even though it's not running? Is there a security setting that I need to tweak?
Does your service have a dependency on another Windows service? If so then it may be that your service is failing to initialize correctly when the machine restarts as it can't make use of the functionality in needs from the dependent service.
This would explain why if you restart the service is works correct.
I was able to solve the problem: the service was failing to initialize properly on windows restart because it was not dependent on any service, when it should have been dependent on the event log service. Setting the "ServicesDependedOn" property of the ServiceInstaller object to "Windows Event Log" solved the problem. Thanks for the help!

Running Windows Service in Foreground

I have a bat file which is installed as a service. I can run the service on a remote box. This service needs to launch another application. The launched application needs to be visible(run in foreground). Currently the launched application is running in background as the windows service is running in background. How can i make the windows service or both the windows service and the application that it launches run in foreground? I intend to manage the service with the ServiceController class in C#.
I think you should probably read
http://asprosys.blogspot.com.au/2009/03/allow-service-to-interact-with-desktop.html
Making a service to just launch another app is a real security issue, what if your other app gets replaced with something else do you have all the checks in your Service to ensure your app is the app you think it is.
A proper approach is to launch the second application in the user's session, while your Windows service app always runs in session 0.
The approach has been part of my discussion with #RaheelKhan under this thread,
How reliable is adding an application manifest to ensure elevated privileges across windows xp, vista and 7?
It requires proper understanding of Windows sessions, session isolation, and platform invoke.

Opening IE through WCF

I have a WCF service hosted in a Windows service.
This service the WCF have one metohd and in this method I have one important line :
Process Browser = Process.Start("iexplore.exe", hostUrl);
I install Windows service as local system, but when I'm trying to invoke that method, everything seems to execute, except that one important line... and IE didn't open.
I would like to add that the method itself is not in the service itself but in one of the service dll reference
Any idea why?
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/63a7d1ec-7077-489a-a250-f7422f04317b
" in order to get the service to actually show the UI, you'd have to set the service in Computer Management to allow it to interact with the desktop. In the services window in computer management, go to the properties of your service, and on the Log On tab, check "Allow service to interact with desktop" "
Since Windows Vista MS has been adding lots of security-related changed esp. in the area what Windows Services can/can't do. Anything "desktop-like" (printing, accessing network shares, using Office Interop etc.) is harder and harder to get working.
You should rethink your design since IMHO any "server-like process" (for example a WCF service) can be accessed in parallel by multiple requests and thus should NOT use processes which are NOT designed for this type of interaction... what happens if your webservice starts multiple IE instances that way ? Will IE behave as you need/expect it ?
IF you really really MUST do it this way you should have
a normal desktop process hosting the WCF service
OR
two processes, one your Windows Service and one running as a normal desktop process dealing with IE... these two process communicate via IPC
Under what user is the service running? Try running it under the currently logged in user, with privileges to interact with the desktop and see if that helps.
In general, its not a great idea to have services launching GUI processes. For example, what if no one is logged on. What if many people are logged on? Should it open in all sessions... etc. Have you considered exposing a simple (e.g. Net.NamedPipes) endpoint on your service, and writing a small client UI to interact with it?

C# Windows Form with Attached Windows Service

I know how I can create a Windows Service in C# but I have a series of variables that the user currently feeds into a text file and I wanted to control this process. So can I attach a Windows service to a Windows form application to control such things as installing the service, uninstalling the service, start and restart. Or is it better to have them completely separate and just use the form app to run system commands for install/uninstall and use the serviceController to start/restart?
Thanks
I suggest to make it completely separated.
It's probably better to keep them separate: have the form write a config file for the service, then either restart the service or have the service detect the changed file and reconfigure itself.
If the service interacts with the desktop, then you'll have to start worrying about what happens if they're running as different users, or if there isn't a logged-on user at all, or...
You can do it by allowing the service to interact with the desktop. It's an acceptable way of doing it, but saying that, I agree with dario-g!

Categories