To create a windows service with a setup project for a service can easy way to install and start up the service after the service is installed.
here both windows service and setup project are more works have to do.
first of all we have to create a windows service project and add some code for setup program so that
it capable installation the windows service and then to start it up.
Create a Windows Service Project
- In the Add new projcet dialog select Windows Service Template to create new service project.
- Right click service1.cs and click View Code
- In the OnStart event handler, replace the comments with anything code you want the service to run.
- In the OnStop event handler, replace the comments with the code if you need to free the resources.
- For the service installation, double click service1.cs to open Design View, In the Design View Right click and select Add Installer in this step a new class ProjectInstaller.cs will add to the project, two elements serviceProcessInstaller1 and serviceInstaller1 you can see in the ProjectInstaller.cs Design View.
- In the Properties of serviceInstaller1, change the ServiceName, DisplayName and Description properties for you need.
- In the Properties of serviceProcessInstaller1, change the Account to LocalSystem.
Note: If you want override OnShutdown Event in the Service, you have to use LocalService Account, Otherwise like LocalSystem the OnShutdown Event will never be invoke.(LocalService and NetworkService values are available only in Microsoft Windows XP) - If you want the service start up automatical at every tiems system start, you have to change the property StartType to Automatic.
If you want the service startup when the service end of it installation, there more steps start from step 9 below you have to do. - Add new class as a Install class into the project, open it in code view, Override the Comit Method as below:
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);
ServiceController[] scServices = ServiceController.GetServices();
foreach(ServiceController sc in scServices)
{
if(sc.ServiceName == "yourServiceName")
{
if(sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
break;
}
}
}
}
foreach(ServiceController sc in scServices)
{
if(sc.ServiceName == "yourServiceName")
{
if(sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
break;
}
}
}
}
Create a Setup Project for Windows Service
1. In the Add new projcet dialog select Setup Project Template to create new setup project.
2. Open the properties panel of the project, in the properties panle change the Manufacturer, ProductName to your fit, through the
installation all the files will copy to the default folder which is composite from Program file/[Manufacturer]/[ProductName].
but you can change the default install location setting in Properties of File System->Application Folder.
but you can change the default install location setting in Properties of File System->Application Folder.
the RemovePreciousVersions is false by default, indicate when you reinstall the program, the old version won't be uninstall.
there are some more properties have to concern like Version, Author, Description, Subject etc.
2. In the Solution Explorer, Right click the projcet name and select View->File System, right click in the Application Folder
and select Add->File or Add->Folder to add anything files you want to add to the folder where you service installed. you also
can select Add->Project Output to specific any other projects output file into this setup project.
3. This step can install mySev.exe to windows system as a service. In the Solution Explorer, Right click the projcet name and select
View->Custom Actions, Now a Select item dialog will show up to you, in the Look In Dropdown list select the file of
window service mySev.exe in the Application Folder and then select ok. the file must been add by step 2.
4. In the Solution Explorer right click Project Name and select Properties to open the Project Properties Page, Click the buttom
Prerequister, checked all .NET Framework version you need and uncheck the all you don't.
If any version you select in the prerequist is missing in the installation computer, the install program will popup dialog to installing
user to request install the properly version of .NET Framework.
For debug or some reason you can use Installutil.exe to install or uninstall the windows service by manual
Installutil.exe is only for .net, can not be uses in earlier windows service develop by C or C++.