If you pointed out the obvious, if you have C / C ++ / Java background, I think C # offers you the lowest entry point.
Assuming you are using Visual Studio 2008, you can do the following:
- Open Visual Studio 2008 and select the menu item "File | New | Project".
- In the New Project dialog box ...
- Select Visual C # Types | Windows node in project types
- Select a Windows Service Template
- Enter a name and location for your project.
- Click OK
- At this point, you have all the basics for a Windows service. The Program.cs file contains the Main () method for your service, and Service1.cs defines the System.ServiceProcess.ServiceBase component, which is your new Windows service.
- In the Property Grid component for your Service1 component, consider setting the following properties at a minimum:
- (Name) - give an object an intuitive name, for example, ServiceExample
- AutoLog - set to
false to prevent the default events from being written to the application event log (Note: I am not saying that you should not log service events, I just prefer to write to my own event log instead of the application log - see below) - CanShutdown - set
true if you want to handle system stops - ServiceName - Defines the name by which your service will be known to the Service Control Manager (SCM)
- In the code for ServiceExample, the virtual functions OnStart () and OnStop () are crossed out. You will need to fill them with what is needed for your service. If you changed the CanShutdown property to
true , you will also want to override the OnShutdown method. I gave an example illustrating the use of these functions. - At this point, the ServiceExample service is essentially complete, but you still need a way to install it. To do this, open the DesignExample component in the designer. Right-click anywhere in the designer panel and select the Add Installer menu item. This adds the ProjectInstaller component to your project, which contains two additional components: serviceProcessInstaller1 and serviceInstaller1.
- Select the serviceProcessInstaller1 component in the designer. In the Property Grid, consider setting the following properties:
- (Name) - give your object an intuitive name, for example serviceProcessInstaller
- Account - Select a LocalService account at a minimum, but you may need to use a NetworkService or LocalSystem account if your service requires more privileges.
- Select the serviceInstaller1 component in the designer. In the Property Grid, consider setting the following properties:
- (Name) - give an object an intuitive name, for example, serviceInstaller
- Description - a description of the service that will be displayed in SCM for your service.
- DisplayName is a friendly name for your service that will be displayed in SCM for your service.
- ServiceName - make sure that this is the same name that you selected for the ServiceName property of your ServiceExample component (see step 4).
- StartType - Indicates whether you want the service to start automatically or manually.
- Remember that I said that I prefer to write events to my own event log instead of the application event log. To do this, you will need to replace the default default EventLogInstaller in ProjectInstaller with a custom one. Make your code for ProjectInstaller as follows:
using System.Diagnostics; [RunInstaller(true)] public partial class ProjectInstaller : Installer { public ProjectInstaller() { InitializeComponent(); EventLogInstaller installer = FindInstaller(this.Installers); if (installer != null) { installer.Log = "ServiceExample";
At this point, you can create your project to get the Windows service executable. To install the service, open a Visual Studio 2008 command prompt and change to the Debug or Release directory where your executable is located. At the command prompt, type the following: InstallUtil ServiceExample.exe . This will install your service on the local computer. To remove it, at a command prompt, type the following: InstallUtil / u ServiceExample.exe
While your service is not running, you can make changes to your service and rebuild, i.e. You do not need to delete your service to make changes to it. However, you will not be able to overwrite the executable with your patches and improvements while it is running.
To see your service in action, open the ServiceExample.cs file and make the following changes:
using System.Diagnostics; public partial class ServiceExample : ServiceBase { public ServiceExample() {
After starting the service with these changes, you can look at the ServiceExample event log in the event viewer and see the logged messages there.
Hope this helps.
EDIT: If you prefer to use the application event log for event logging instead of custom, just do not make any changes to the ProjectInstaller.cs file. Also, leave a line that sets the EventLog log property in the ServiceExample constructor. When the service starts, log messages will appear in the application event log.
Matt Davis Feb 27 '09 at 7:08 2009-02-27 07:08
source share