Windows Services are applications that run in the background and provide some functionality to the system. They have virtually no user interface – except Windows’ built in “start” and “stop” functionality and a few settings to control security, etc. This makes them great for standalone processes that ‘just work’ and require no user intervention, but it also makes them notoriously difficult to debug and develop. Because of this, I typically create two projects when creating windows services – the windows service for production, and a console app for development. However, recently, I came up with a way to combine the two, so that I can have both the windows service AND the console app in the same project. Here’s what I did:
First, in general, it’s good to separate your actual logic into a separate class library that contains the bulk of your service logic. I name this something like “MyCompany.App”, and generally have one class that the Windows Service can use fairly easily– that is, it has a Start and Stop method.
Next, add a Windows Service project and in the ServiceBase code that is generated, new up the above class and use it:
Add a ProjectInstaller (open the service in design mode, view properties, and click ‘Add Installer’), and you have the windows service, ready for installation with installutil.exe. To wire up the console app, a few more steps are required. See, Windows Services are .exes, just like any other app – they just happen to follow a certain convention that makes them Windows Services (namely, they have a ProjectInstaller and one or more instances of ServiceBase). As long as we follow that convention, Windows is happy, and we can do whatever else we want. So:
- Right click the Windows Service project and click ‘properties’
- Set Output type to ‘Console Application’, and Startup Object to the ‘Program’ class.
- Under ‘Debug’ enter ‘-console’ in the ‘Startup Options’ ‘Command Line Arguments’ section
- Inside the ‘Program’ class, add a switch to enable console mode
And that’s it! The end result is a command line app that can be run using the ‘-console’ switch, or installed as a Windows Service. When debugging locally, it’s run as a console app. Ideally, you would build a Setup package to install it, but to install manually, you would use the installutil tool that ships with .NET.