Managing Windows Service (creating, deleting, starting, stopping, arguments) - c#

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

Related

Is it possible to recognize if exe is Windows Service?

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.

Windows services does not run my class method

I have a windows service application which would scan the wifi list and save to file. then i will read the file and write to mysql database and close the file. my wifi scanning and db methods in another class within same windows service project which i invoke in service class. I am able to run and install the windows service but i think those methods are not called. I have even tried them in the service class but they still didn't call up in OnStart method. When i run it section mode and debug from visual studio it works fine but when i install it in installutil it only starts the service nothing happens though the service still runs in the background. I've created windows form version of this and that works fine.
I think it's more likely that the class method is called but writing the output file fails. The account that is used to run the service likely has no rights to create and/or write the output file. Check the folder that it should be written to and see if the account that runs your service has proper access rights. You can also use Process Monitor to verify this (set a filter on Path or Process Name).

Configurable Windows Service - How and where to store configuration

I created a C# application to manage data synchronization between an ERP and a CRM. This application reads a table every 500ms, and sends data via WebService to CRM.
For the moment, I have two screens for my application : a first to configure connections (ERP's DB connection informations and CRM's URL and WS Token), a second with a start button to launch the loop thread.
I know want my application to be nothing more than a windows service installer. What I want to do is to launch my application, configure ERP and CRM connections, and then click on a 'Install' button. When this button is clicked, a service is created and makes the same work that my application makes now when i click the 'start' button.
I already read those links Converting my application in a Windows Service, Making an existing exe with gui into windows service and of course http://msdn.microsoft.com/en-us/library/d56de412%28v=vs.100%29.aspx.
Here is my question :
How to make a configurable windows service ? I think the simplest way is to store configuration into files that utility and service would share. Where to store those files ? The utility must be able to find these files afterwards : registry ?
Thanks,
It's a common practice in .NET to store application's or service's settings in this relative path Environment.SpecialFolder.ApplicationData

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.

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