Sunday, June 14, 2009

Programmatically Install Window Service

This article is about to install / uninstall window service programmatically from C#. This is useful when you need to install / uninstall window service from some other application.

For this purpose you need to use ServiceProcessInstaller and ServiceInstaller class from another application. These two installer classes are responsible for Installation of service.

C# Example for Programmatically Install Window Service.

ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
ProcesServiceInstaller.Account =  ServiceAccount.User;
ProcesServiceInstaller.Username = "<<username>>";
ProcesServiceInstaller.Password = "<<password>>";

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new System.Configuration.Install.InstallContext();
String path = String.Format("/assemblypath={0}", @"<<path of executable of window service>>");
String[] cmdline = { path };

Context = new System.Configuration.Install.InstallContext("", cmdline);              
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.DisplayName = "MyService";
ServiceInstallerObj.Description = "MyService installer test";
ServiceInstallerObj.ServiceName = "MyService";
ServiceInstallerObj.StartType =  ServiceStartMode.Automatic;
ServiceInstallerObj.Parent = ProcesServiceInstaller;          

System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
ServiceInstallerObj.Install(state);

C# Example to Programmatically Uninstall Window Service.

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new InstallContext("<<log file path>>", null);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "MyService";
ServiceInstallerObj.Uninstall(null);

other article related to window service.

http://dotnetstep.blogspot.com/2009/06/passing-parameter-to-installutil.html

http://dotnetstep.blogspot.com/2009/06/install-window-service-without-using.html

http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html

http://dotnetstep.blogspot.com/2009/06/get-windowservice-executable-path-in.html

Please give your idea about this article.

5 comments:

Anonymous said...

Thanks for providing the wonderful script. But I have quick question, do we need to put this code in the same windows services file or we need to create one more class file for this?

Thanks

dotnetstep said...

This post is specially for installing window service from another application.
Please look at my another post.
Install WindowService using installutil.

To add installer class to window service directly.

Mohammad Rastkar said...

Worked...Thanks.
It's a great approach!

Anonymous said...

The uninstall worked. Thanks!

Mike Henderson said...

I added this code to the Main call of my service to allow programmatic control of the application and it worked fine. With just a few additions the application can be self installing with options to login via a specific login/password and to change the service name (in case you need multiple of the same EXE that do different tasks).

Thanks a lot, this was simple and straightforward!