Introduction
Recently I came across a situation where I had to do three things:
- Create WCF Windows service.
- Create a setup project for the service.
- 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:
ServiceProcessInstaller
, andServiceInstaller
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);
}
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.