Today I'll show how to build Windows Service in C# using Visual Studio 2010.
First, open Visual Studio and create Windows Service project.
Visual Studio will create a WindowsService.cs file, let's add some code to it:
public WindowsService() { this.ServiceName = "My Windows Service"; this.EventLog.Source = "My Windows Service"; this.EventLog.Log = "Application"; this.CanHandlePowerEvent = true; this.CanHandleSessionChangeEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanStop = true; if (!EventLog.SourceExists("My Windows Service")) EventLog.CreateEventSource("My Windows Service", "Application"); } static void Main() { ServiceBase.Run(new WindowsService()); } protected override void OnStart(string[] args) { base.OnStart(args); File.Create(@"C:\servicefile.txt"); }
My windows service simply creates a file when it starts, you can write any other logic instead. Now we need to add an Installer for our service. Mouse right-click on the project to add another class, let's call it WindowsServiceInstaller.cs, put this code inside of it:
[RunInstaller(true)] public class WindowsServiceInstaller : Installer { public WindowsServiceInstaller() { ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); ServiceInstaller serviceInstaller = new ServiceInstaller(); //# Service Account Information serviceProcessInstaller.Account = ServiceAccount.LocalSystem; serviceProcessInstaller.Username = null; serviceProcessInstaller.Password = null; //# Service Information serviceInstaller.DisplayName = "My Windows Service"; serviceInstaller.StartType = ServiceStartMode.Automatic; // This must be identical to the WindowsService.ServiceBase name // set in the constructor of WindowsService.cs serviceInstaller.ServiceName = "My Windows Service"; this.Installers.Add(serviceProcessInstaller); this.Installers.Add(serviceInstaller); } }Ok, we have the Installer, so our service can be registered in Windows OS. Now we need to add to our solution a Setup Project which will actually install and uninstall our windows service.
Once you done, mouse right click on the setup project and choose Add Project Output:
Then click on Primary Output and choose your Windows Service project as shown below:
Then, mouse right click on Setup Project and click View -> Custom Actions:
Add Primary Output to each folder in Custom Actions as shown below:
Now build your Windows Service project and then build Setup Project. Once build succeeded you are ready to install your windows service. Mouse right click on Setup project and choose Install (you can also choose then Uninstall when needed):
To check if you have installed your windows service, run Service manager:
Look if the service exists in the service list:
Download the source code of this example (Visual Studio 2010 project).




Thank you for this clear and understandable article. I usually find that it's simpler to explore something by example, rather than reaching the guides.
ReplyDeleteAbsolutely agree with you, Karina.
DeleteThanks , very clear and concise.
ReplyDeleteVery easy to understand the first part but could you explain how to get the Setup Project? I'm using VS 2012 and I do not have it.
ReplyDelete