Caveman's Blog

My commitment to learning.

Installing Dotnet Windows Services (the easiest way).

with 4 comments


Introduction
I will demonstrate the easiest way of installing a windows service. Using the InstallUtil.exe has been my primary mechanism of installing a windows service until I came across Windows Services Can Install Themselves by W. Kevin Hazzard. That article has demonstrated a service can be installed/uninstalled/launched from the command line alone. I on the other hand have come up with a way to install/uninstall the windows service by double clicking the service executable.

Traditional command line approach: C:\> InstallUtil MyWinService.exe
Kevin Hazzard’s command line approach: C:\> MyWinSvcHost.exe -install
My approach: Just double click on the Service exe.

How the code works

The first double click on the service executable will install the service and the second double click on the service executable will uninstall the service.

When ever we run a windows service, the main method gets executed as this serves as the entrypoint for the service. The list of of existing windows services can be fetched using the GetServices method of the Service controller class. Loop through the array of services to determine if our service is already installed.

ServiceController[] services = ServiceController.GetServices();

foreach (ServiceController service in services)
{
if (service.ServiceName.Equals(SERVICE_NAME))
{
_IsInstalled = true;
}
}

If the service does not exist on the machine we will install it by executing the InstallMe method of the SelfInstaller class, followed by a messagebox notification of the same.

SelfInstaller.InstallMe();
MessageBox.Show("Successfully installed the " + SERVICE_NAME, "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);

If the service does not exist on the machine we will uninstall it by executing the UninstallMe method of the SelfInstaller class, followed by a messagebox notification of the same.

SelfInstaller.UninstallMe();
MessageBox.Show("Successfully uninstalled the " + SERVICE_NAME, "Status", MessageBoxButtons.OK ,MessageBoxIcon.Information);

As Kevin said in his article this style of installing the service without using the InstallUtil.exe provides us with many options of installing and invoking the service. Happy coding my dear fellow s/w brothers. There might be a downside to this approach; what is someone double clicks on the exe in mission-critical environment [3] ? Situations like that can be prevented by prompting the user if he/she would really likes to Install/Uninstall the service. Another cool suggestion from PIEBALDconsult is to develop a windows form inside the service that would actually allow the user to install/uninstall and control the state of the windows service.

Update

Version 2.0: Code is located in CoolService_V2.zip

a) Added SMESSER‘s [4] code to get rid the annoying alert box (Cannot start service from the command line or a debugger.) that appears after the service has been installated/uninstalled.

b) Added code to make this approch more secure (Thanks to PIEBALDconsult‘s concern) by prompting the user, is he/she REALLY likes to install/uninstall the service.

c) Moved the WSInstaller class to a WSInstaller.cs for a cleaner code.

Update: 9/21/2011

Problem: Exception when installing on Win 7: System.ComponentModel.Win32Exception: No mapping between account names and security IDs.

Solution: Add the following code to the InitializeComponent() method of the ProjectInstaller.cs file.

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

References:
1. MSDN
2. Windows Services Can Install Themselves by W. Kevin Hazzard
3. PIEBALDconsult (A fellow member at Code Project)
4. SMESSER (A fellow member at Code Project)

kick it on DotNetKicks.com

About these ads

4 Responses

Subscribe to comments with RSS.

  1. Very well done! i was trying to do the same thing but didnt getting any any any idea. you are genius…

    thanks

    Khayyam Sikander

    Khayyam Sikander

    September 2, 2009 at 1:58 am

  2. Excellent solution for windows service install/uninstall.
    This approach allows us to manage our services through other applications.

    Hitesh Shah

    September 23, 2009 at 1:31 am

  3. Good and informative post. Very useful demonstration for install/uninstall windows services. Thanks for sharing this info with us. From this approach we can manage our services.

    http://gloriatech.com/microsoft-windows-server-2008-setup.aspx

    Rahul

    January 4, 2011 at 12:32 am

  4. wordpress tutorials…

    [...]Installing Dotnet Windows Services (the easiest way). « Caveman's Blog[...]…

    wordpress tutorials

    February 27, 2012 at 3:24 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: