Is it possible to recognize if exe is Windows Service? - c#

I'm creating simple service manager in C# because we create services as an extensions to application. I'm able to get list of installed services with path to exe file but one of feature in wishlist is to recognize services in given path which are not installed and list them with install button.
Is there a way to recognize if exe file is Windows service?

A service has no special attributes nor properties, the only way to detect if a .exe is a service is to see if it calls one of the service functions. Looking for StartServiceCtrlDispatcher as a string is probably the best option.
Note: If somebody knows that you are looking for this they could easily "encrypt" the StartServiceCtrlDispatcher string and bind to the function at run-time with GetProcAddress.

Related

Managing Windows Service (creating, deleting, starting, stopping, arguments)

What I am trying to do,
i have an executable file (myService.exe). it is a windows service and it supports start, stop and other such options. It uses one argument (filename) and run its survices on it.
e.g. c:\myService.exe someFileName.dat
now i am making a c# windows form application. I am reading filename from a textbox and on button click event i want to do this.
check whether myServices.exe is installed on machine or not.
if it is installed then i have to check it is in running state or not.
if it is in running state then stop it.
after stopping it i need to uninstall the service.
after uninstalling it i want to install it again and have to give filename as the parameter of service and then run it.
How to install a windows service programmatically in C#?
i have tried this but i have to give argument to the service which is not present there.
Why not create a List that the windows application uses or if you are stuck on using a Windows Service application have a .config file that the windows service app uses and store the initial directory in it. using the FileInfo and DirectoryInfo and of GetAllFiles() method store the files in a List and iterate the List varList = new List(); variable object that way.
You could also make a Web Service and have the windows application consume that web service by adding a web reference the same way you would if you wanted to add a reference to an additional / 3rd party assembly. Lots of options you have

Is it possible to create a C# Windows Service that detects new file creation on Windows XP?

I have a folder that will contain a temporary file that will be created by web service. I want the file to be converted by .exe program I have via command line. Now, I have already generated web service that will create file and store in one specific folder.
Here is the scenario:
Web service receive amr byte data from my mobile app.
Web service deletes all files existing in the folder. (to remove .amr file and .mp3 file that was generated from the previous round)
Web service create new .amr file in the folder.
Windows Service detects that there is a new file created. It call converter.exe. Once complete, it creates .mp3 file in the same folder.
Now, I have no problem with step1-3. The problem is step4, which I need to convert .amr file to .mp3 file. In order to do this, I need to pass some dos-command to converter.exe. I found that it is impossible for me to do it from my web service, so I am thinking of creating C# Windows Service that will watch the folder storing the files. Is it possible to create Windows Service that is able to detect new file creation and run converter.exe as soon as a new file is created ? I did search on the Internet and found that I may be create Windows Service with FileSystemWatcher, but i am just not sure if it will be possible.
The reason I found when trying to execute converter.exe from web service is about permission. I did everything but still can't make it success.(Using Web Service, converter.exe is shown on the process list in task manager as local system, but it will not be executed because of some security reason...) Hope there will be no security problem when I try to execute converter.exe using Windows Service.
Thanks in advance for all answers and suggestions :)
System.IO.FileSystemWatcher will do the trick. The Created event will tell you when a file has been created in the directory you're watching. To start your conversion program, take a look at the System.Diagnostics.Process.Start method. Both of these should work fine in a Windows service.

Testing Windows Service in .Net

I'm developing a windows service in C# .Net and i'm having some dificulty to execute and test my service.
I'm using a timer component to run it from time to time and execute my methods. I have to intialize the service all the time to run it.
Anyone know a more practical way to test a service?
This often boils down to the question what you want to test. Do you want to test the service, or the functionality that it performs? Personally I would...
Refactor all of the service functionality into a separate class library
Test the library
Invoke the library functionality from the service
...and then keep the amount of code in the service itself to the bare minimum needed to trigger the functionality in the class library.
Windows services are ordinary EXEs that happen to connect to the service control manager on startup.
I normally test services by giving them an optional command line argument that tells them to execute normally, inside the Main method, instead of acting as a service. That way I can debug them directly within Visual Studio, or on the command line, etc.
You probably want to add unit tests to your service.
It sounds like you could benefit more from adding your application to the Task Scheduler instead of running it as a service.
Other than that if you really need it as a service you need to design it so it can be tested. I usually write my services in a separate class and add a .EXE project to it so I can run it from the command line too.
One of the ways I have run services when developing is along these lines - Run a windows service as a console. I have found it useful because you can add code to write out to the console for debug information and writing out any relevant exception data.
I would not use this method instead of unit tests but I found this to be a useful way to work and debug where needed.
After create window service you can set it into window service.
Install service using InstallUtil.exe
To install or uninstall windows service (which was created using .NET Framework) use utility InstallUtil.exe. This tool can be found in the following path (use appropriate framework version number).
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe
To install .NET service run command similar to this (specify full path to your service).
InstallUtil.exe "path of service"
To uninstall .NET service use the same command with parameter /u.
InstallUtil.exe /u "path of service"

C# register the same windows service with different parameters

I need to create many windows services to host many WCF Service so I can stop a single one (for example to update it) and keep the other running.
Since I don't want to create a windows service for every WCF Service I have, I'm trying to create a C# application that can register itself as a windows service but with different parameters (and service name of course).
For example calling it this way:
MyService.exe /install WcfService1.dll
MyService.exe /install WcfService2.dll
This creates two different windows service like these:
WcfService1 -> MyService.exe /run WcfService1.dll
WcfService2 -> MyService.exe /run WcfService2.dll
I can't find a way to do this other than editing directly the registry.
I've found a nicer way to do it: sc.exe
sc.exe create WcfService1 binPath= "MyService.exe /run WcfService1.dll"
so I just need to create a bat/cmd to launch this command.
By its nature, a service is designed to be the go-to process for dealing with a particular thing, and having multiple instances is generally not a good idea. However, it is possible to do what you want; for instance, you can have multiple instances of SQL Server running at one time.
Have a read of this. Basically, your service installer can be given some command-line or other configurable argument that will tell it the name to which it should register itself in Windows as a service. This is normally automatic based on the service installer's and service executable's properties, but all of this can be set dynamically as well. Change the name, and you change the service registration. The name of the service should be (AFAIK) inspectable by the executable being run as the service, which can tell it the WCF service for which it is responsible.

Passing a Windows Service Parameters for it to act on

I want to turn a program I have into a service so I can use it without logging it. Basically what it does is it backs up specified folders to a specified location using SSH. However the problem I'm running into is I don't know how to tell it these items. I only know how to start, stop, and run a custom command with only an integer with a parameter.
How can I do this?
Windows Service, not a Web Service
edit: The folders it backs up will not remain consistent and will be updated at every runtime
You can instantiate your service and pass command line arguments using the ServiceController class.
using (ServiceController serviceController = new ServiceController(serviceName))
{
string[] args = new string[1];
args[0] = "arg1";
serviceController.Start(args);
}
"arg1" will then be available as regular command line arguments in main() when Windows starts up the service.
I see that you (or someone) voted Sebastian Sedlak's answer down, because he mentioned hosting a WCF Service in the Windows Service. Your reply was
It's in nice bold lettering in the question. Not a Web Service, therefor WCF is out of the question
I think you misunderstood what he meant. He wasn't talking about a Web Service. He was talking about hosting a WCF Service within your Windows Service.
It's far from the same thing. You can host a WCF Service within any Windows (Forms/Console/Service) application. The point of doing so, is that the application is then reachable for communciation via its internal WCF Service, in the same fashion as you can communicate with a Web Service (you can also host WCF Services in IIS, btw, which would then make them "Web Services", in the sense you seem to be referring to).
In a Windows Service, this means you can send any command to it and also get any information you want from it - while it's running.
In fact, I am working on a project right now, which is a Windows Service that I need to be able to contact and pass commands to - and get information from - at runtime. For example, I want to be able to tell it where to store certain things, what to log, to have it reset/restart - and poll it for status messages. I do this by hosting a WCF Service inside the Windows Service. That WCF Service exposes a set of methods, that in my case includes receiving commands and returning status information. So when the Windows Service is running, I can contact it (even remotely), via its built-in WCF Service and tell it what to do.
This an extremely easy thing to implement, and in the case of Windows Services, can provide you with a much richer interface to the Service than through the basic standard commands.
However, you specified that you wanted your Windows Service to receive its folder settoings each time it starts up, which makes such a passive setup less than ideal (as it would be unable to do anything until you passed it the right folders).
One way to deal with this (using a hosted WCF Service), would be to have the Windows Service running all the time (i.e. automatic startup). Its default state would be idle. Then you could issue it a "start processing"-command, feeding it the correct folders to work on (through a call to the corresponding WCF Service method). Similarly, the WCF Service would expose methods giving you the status of the application (current folder, progress, busy/idle etc). Once the processing is done, it would go back into the idle state, waiting for the next set of folders to be supplied to it.
Doing it this way would make it very easy to control remotely - you could even make an online administration panel for it, accessible from anywhere.
The issue, is that, while passing in parameters is not difficult, when the machine restarts and windows tries to restart the service, those parameters are not there. they only exist when someone starts the service from the command line.
for example. I have a windows service which hosts a WCF service. I want the users to be able to specify a non-default port number for the WCF service to listen on. They do this by starting the windows service like so... MyService -port:xxxxx
Which works fine, until the server is rebooted, then windows restarts MyService (but without parameters) and the wcf service defaults to original port #
Any service is capable of receiving command line arguments at start-up.
Would it be possible to use a configuration file to specify these items?
Store the service's startup parameters in the registry: and then, when the registry starts, it should read its startup parameters from the registry.
Windows services have executables like any other. I believe you can write it to accept command-line parameters and specify those parameters in the Windows Service configuration. You can also have it read a config file. If you're using .NET, there are config file classes in the framework.
Why not just Host a WCF Service in the Windows Service to obatain such "admin" functions?
(Remoting is also possible)
RE: config file.
Of course a config file can be used.
And the file can be changed while the service is running.
This would be a nice solution if the config file changes in fact.
All my services use an XML config file, wrapped in a class for easy reuse.
The wrapper has an option to monitor the XML file using fileMonitor for changes, optionally refreshing the content of the config file automatically, and finally raises an event to the service class.
The service then has the option of "resetting" itself as needed to incorporate the new values in the XML configuration file.
Placing configuration into the registry has a few issues:
Security (ie: installer being granted access), depending on what tree is used
The service will not be aware of changes
Portability - although minor as the install should setup registry settings
Where an XML file is easy to copy, edit, share, view and understand.
Throw in some good COMMENT blocks and a detailed XSD file, and it becomes a source of good documentation too.
Look into XPath for easy navigation and extraction of values within the XML file.
$0.02
... david ...
Concerning the app.config file - I'm rather sure that your service will read and use those files as I write all my windows-services this way ;)
So just put everything you need in the app.config under "application" (not user) and put allway edit the "yourname.exe.config" in the folder where you "InstallUtil" the service from.

Categories