- public partial class WinService : ServiceBase
- {
- public WinService()
- {
- InitializeComponent();
- }
- protected override void OnStart(string[] args)
- {
-
- }
- protected override void OnStop()
- {
-
- }
- }
Go to Solution Explorer and right-click on WinService.cs then go to the view designer then look at the design view then right-click on the design page and click on “add installer” and it will look like this:
After adding the ProjectInstaller.cs you have 2 components in the design view of the ProjectInstaller.cs (serviceProcessInstaller1 and serviceInstaller1). You should then set up the properties as you need.
Click on serviceinstaller1 and go to the properties in the right pane and edit the service name Service1 to WinService as in the following:
Click on serviceProcessInstaller1 and go to the properties in the right pane and select Account and choose LocalService from the dropdown and save. See the following:
You have two assemblies under References as in the following highlighted (1) System.Configuration.Install and (2) System.ServiceProcess.
Create Setup ProjectBuild your service project, you need to set up the project to install the build/compiled project and run the installers to run the Windows service. Create a new project as a “Setup Project” and you need to add project output.
Create Setup Project for Window ServiceGo to the Solution Explorer and right-click on the solution, go to Add > New Project.
Open a dialog box, go to left pane under Installed Templates > Other Project Types > Setup and Deployment > Visual Studio Installer and go to the right pane and select the project as a “Setup Project” and click on the OK button.
Custom actions add to setup project
Go to the solution, right-click on the setup project then select View > Custom action.
The “Custom action” editor appears.
Right-click on “Custom action > Add Custom action”.
Open a dialog box “Select Item In Project” then double-click on “Application Folder” then click on the “Add Output” button. Open a dialog box, choose your project (Window service) and “Primary Output” from (Active) and click on the OK button.
Build your setup project and install the Windows service.
Install And Start The ServiceGo to the Solution Explorer then select Build for both projects (Windows Service and Setup Project) then right-click on the Setup Project and click on the Install option and follow the setup wizard procedure. Finally your service is ready for being started and stopped. If you want to start the Windows server then go to Start > Administrator tools > Service > Find Your Service then right-click on the Service Name then select Start. Otherwise go to Start > Computer (My Computer) > Manage > Click on Service And Application from the left pane > Services.