Click here to Skip to main content
Click here to Skip to main content

How do I start a .NET WCF Windows Service automatically soon after the setup installation is complete

, 23 Jan 2012
Rate this:
Please Sign up or sign in to vote.
Start a .NET WCF Windows Service automatically soon after the setup installation is complete.

Introduction

Recently I came across a situation where I had to do three things:

  1. Create WCF Windows service.
  2. Create a setup project for the service.
  3. Start the service as soon as the installation is completed.

The first two steps are fairly straightforward, and there are a lot of online contents which helps us in doing so. But the information of how to do the 3rd step was not easily available. Hence I thought I would put down an article which helps people do so - i.e., start the Windows service automatically as and when the service installation is complete.

I will just brief through the first two steps to get the article started off, and then come to the 3rd point with the code snippet.

1. Create a WCF Windows service

http://msdn.microsoft.com/en-us/library/ms733069.aspx. This is a very good MSDN article which explains clearly as to how to create a WCF Windows service.

Why do we need a WCF Windows Service?

Usually Windows services are considered as background programs which just do their job without any user intervention. But what is so special about a WCF Windows service?

Let me explain this with an example - Consider I have a requirement where I need a background service which looks into a database and creates a particular formatted file for me in certain intervals based on the time of the day. So for this requirement, I can create a normal Windows service that keeps looking at the database at pre-defined time of the day in intervals and generates the files for me.

Now imagine, suppose there is a slight change in requirement - users not only want to generate files at the particular intervals/time of day, they also want to build another client application that creates those files on-demand. They also want to change the pre-configured interval from the client. i.e from the click of a button, they want to generate the files irrespective of the pre-defined interval configurations.

This is a situation which requires the services of the Windows service to be exposed outside - hence a WCF Windows Service.

2. Create a Setup project for the Windows Service

http://msdn.microsoft.com/en-us/library/zt39148a(VS.80).aspx Another walk-through MSDN article which explains how to create a setup project for a Windows service.

This step creates an msi file which does two things:

  • Installs the primary output of the service to a particular folder.
  • Installs the windows service in the Service Control Manager (SCM).

3. Start the service as soon as the installation is completed

Remember my requirement was to start the service soon after the service installation is complete. This section explains how to do that.

Basically when you create the WCF Windows service, you will have to add two components to the Project Installer:


  1. ServiceProcessInstaller, and
  2. ServiceInstaller

This is explained in the first link and this is a basic step towards creating any .NET Windows service.

All I do is to handle the 'committed' event of the ServiceInstaller component. This event is fired once the service installer commits the installation of the Windows service.

The code snippet below shows how exactly to do that. The ProjectInstaller class will already be available in your code base as part of the ProjectInstaller.cs file.

[RunInstaller(true)]
public partial class ProjectInstaller : Installer 
{
    public ProjectInstaller()
    {
        InitializeComponent();
        this.serviceInstaller1.Committed += new InstallEventHandler(serviceInstaller1_Committed);
    }
    /// <summary>
    /// This method will ensure that the Service is started once installation is committed.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void serviceInstaller1_Committed(object sender, InstallEventArgs e)
    {
        var controller = new ServiceController(this.serviceInstaller1.ServiceName);
        controller.Start();
        controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
    }
}

Once the committed event of the ServiceInstaller is handled as shown below, the setup project created in step 2 should be re-built. Now when you install the Windows service using the Setup, the Windows service is not only installed, it is also started for you.

Conclusion

Usually people will create another batch file which is used to start the service in one click. Alternately, the user can open the SCM (services.msc from command line), and then scroll and find their installed service, and then right-click and start.

This solution will allow the user to perform whole installation and start of the Windows service in one step.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abey Thomas
Technical Lead Philips
India India
I am Abey Thomas Raju from Bangalore, India.
 
More than 7 years of solid experience in designing, developing and maintaining web applications using .NET technologies.
Good knowledge of ASP.NET aspects and related web technologies like Javascript, AJAX, CSS and jquery.
 
Proven experience in Object oriented analysis and design by developing and maintaining top class back-end framework for successful financial web applications.
Follow on   Twitter

Comments and Discussions

 
QuestionPlease upload the Code PinmemberRamanIndia10-Sep-13 20:58 
GeneralWorks great PinmemberMember 851307111-Jul-12 5:02 
GeneralReason for my vote of 4 excellent! you have given a "What an... PinmemberMember 779575530-Jan-12 22:15 
GeneralRe: You are welcome.. PinmemberAbey Thomas1-Feb-12 16:46 
GeneralReason for my vote of 5 very interesting! I'm going to deve... Pinmembersaidmr19-Jan-12 8:12 
GeneralRe: You are welcome! Good to know this tip helped someone :) PinmemberAbey Thomas19-Jan-12 14:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 23 Jan 2012
Article Copyright 2012 by Abey Thomas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid