1. Open the project which you want to host as the windows service. (Also can create an empty Windows Service project)
2. Right click the project and add the Windows Service.
3. Go to the Windows Service cs class OnStart method, call the corresponding method.
protected override void OnStart(string[] args)
{
Thread thread = new Thread(new ThreadStart(StartListening));
thread.Start();
}
4. Go to the designer view of Windows Service and right click to add the Installer Class.
5. Open the created Installer Class and go to the Designer.cs to add the following code.
The ServiceName need be changed accordingly to the WindowsService name just created.
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "JMSListeningServiceR52";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// JMSServiceInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceInstaller1,
this.serviceProcessInstaller1});
}
#endregion
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
6. Create a Setup Project
7. Right click the project and add the Project Output. Need select the correct project.
8. Right click the project and click View--> Customs Actions
9. Add the Customs Action step6 created for all 4 items (Install, Commit, Rollback, Uninstall)
10. Build the projects and try install the Setup Project.
More details can refer to http://www.sarin.mobi/2008/08/c-windows-service-visual-studio-2008/