1. Create a new Windows service
2. Add Installer
This step is important after you have finished processing your business logic and need to add a installer to be installed by your Windows service.
Add Installer to vs
Right-click your service, select View Designer
Then right-click Add Installer in the View Designer view
So the installer is added well.
3. Set Service parameters
A ProjectInstaller.cs is automatically generated when installer is added, and there is a InitializeComponent method in this file, as follows
[CSharp]View Plaincopy
- Private void InitializeComponent ()
- {
- This.serviceprocessinstaller1 = new System.ServiceProcess.ServiceProcessInstaller ();
- This.serviceinstaller1 = new System.ServiceProcess.ServiceInstaller ();
- //
- ServiceProcessInstaller1
- //
- This.serviceProcessInstaller1.Password = null;
- This.serviceProcessInstaller1.Username = null;
- //
- ServiceInstaller1
- //
- This.serviceInstaller1.ServiceName = "Service1";
- //
- ProjectInstaller
- //
- This. Installers.addrange (new system.configuration.install.installer[] {
- This.serviceprocessinstaller1,
- This.serviceinstaller1});
- }
- This.serviceProcessInstaller1.Password = null;
- This.serviceProcessInstaller1.Username = null;
To set the login password for the service, if you do not want to set the username password, you can also run the service with the local System account code as follows:
This.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
Other settings can also be done here.
4. Installing and uninstalling Windows Services
After the finished Windows service is published is an EXE file, in order to enable the service on the machine used, we will use the tools provided by Microsoft InstallUtil Tool, the command line to install and uninstall the service.
InstallUtil tools in the directory: System disk: \windows\microsoft.net\framework\v4.0.30319, run cmd, enter
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil Xxxx.exe Enter to complete the installation of the WINDOWS service.
The unload is the input c:\windows\microsoft.net\framework\v4.0.30319\installutil/u xxxx.exe carriage return.
5. When running the Windowservice service after installation, it is sometimes possible to encounter the situation that the service is automatically down, in which case the EXE file at compile time is x64 or x86. So we use Mubuild manual build into anycpu EXE service, may be resolved.
VS2010 Creating the Windowsservice service