Click here to Skip to main content
13,456,114 members
Click here to Skip to main content
Add your own
alternative version


178 bookmarked
Posted 9 Apr 2003

Creating a C# Service Step-by-Step Lesson II

, 9 Apr 2003
Rate this:
Please Sign up or sign in to vote.
A multi-article contribution describing in step-by-step detail on creating your own service with integrated support for setup and custom event logs. This lesson we'll add multiple child services as well as updating the installer to install these services as well.


Recently I was trying to write a C# service using the .NET framework and ran into a lot of issues that were not covered in the documentation or if they were, the solution wasn't easily inferred. So I decided to write a series of articles describing in step-by-step detail, how I went about solving the problems I encountered.

This is a multi-article contribution which it broken down as follows:

  1. Creating the project, initial service and installation
  2. Adding additional services to the application
  3. Adding support for custom event logging

Using the code

Starting with the code we developed during the last lesson, we are going to create some child services that are responsible for different aspects of the Spades game server. During this lesson, we are going to create a Login service, Lobby service and Game service. All these services will derive from the SpadesChildServiceBase class that we developed in the last lesson. We will also need to create an instance of these classes in the Application object, as well as create different ServiceInstaller classes to install the 3 different child services.

So without further delay, lets get started creating our first child process. All the child processes will be similar in structure, so I won't list the source code for each one, but the basic difference is just the class name. So let's go ahead and create a new class and call it SpadesLoginService and derive it from SpadesChildServiceBase. Your code should look like this when you are done.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace CodeBlooded.Spades.Services
    public class SpadesLoginService : SpadesChildServiceBase
        public SpadesLoginService()
            this.ServiceName = "SpadesLoginSvc";

        /// <SUMMARY>
        /// Set things in motion so your service can do its work.
        /// </SUMMARY>
        protected override void OnStart(string[] args)
            base.OnStart( args );
        /// <SUMMARY>
        /// Stop this service.
        /// </SUMMARY>
        protected override void OnStop()

        /// <SUMMARY>
        /// </SUMMARY>
        /// <RETURNS></RETURNS>
        protected override int Execute() {
            // for right now we'll just log a message in the
            // Application message log to let us know that
            // our service is working
                                        ServiceName + "::Execute()");

            return 0;

Since we want our child service to start the admin service if it's not already started, we need to add this code to the SpadesChildServiceBase class. We will also need to add some shutdown code to the SpadesAdminService class to shutdown all the child services when we stop the admin service.

Open up the SpadesChildServiceBase.cs file and in the OnStart method, we will use a ServiceController to gain access to a specific service on a specific computer and control the service via the Start or Stop methods. Just add the following code to your OnStart method of the SpadesChildServiceBase class.

protected override void OnStart(string[] args)
    // gain access to the SpadesAdminSvc on the local computer.
    ServiceController controlAdmin = 
       new ServiceController("SpadesAdminSvc", ".");

    if( controlAdmin.Status != 
        System.ServiceProcess.ServiceControllerStatus.Running &&
        controlAdmin.Status != ServiceControllerStatus.StartPending ) {
        // start the admin service

    base.OnStart( args );

We need to create an instance of the SpadesLoginService class in the Application class, in the Main method. The code looks like:

servicesToRun = new ServiceBase[]{    new SpadesAdminService(),
                new SpadesLoginService() };

All we have left to do at this point is, create another ServiceInstaller class in our SpadesInstaller class. The code looks like the following and is just like the code we wrote in the previous article. The only thing different in the child services is that, we configure the installer to add the ServiceDependOn keys to the registry. Since this key is a MULTI_SZ type in the registry, it can hold multiple strings, so the way we do it in .NET is to create a string array and add the different strings to the array. Since we really only have one service that the child services depend on, we only need the one entry of SpadesAdminSvc.

public SpadesInstaller()

    // ...

    ServiceInstaller serviceLogin = new ServiceInstaller();

    serviceLogin.StartType    = ServiceStartMode.Manual;
    serviceLogin.ServiceName    = "SpadesLoginSvc";
    serviceLogin.DisplayName    = "Spades Login Service";
          = new string[] { "SpadesAdminSvc" };

    // ...
    Installers.Add( serviceLogin );

Go ahead and create 2 more classes, one SpadesGameService and SpadesLobbyService both of which derive from SpadesChildServiceBase. Be sure to change the ServiceName property in your constructor. In our case, they should be SpadesGameSvc and SpadesLobbySvc respectively.

You will also need to create an instance of each in the Application::Main method and add them to the servicesToRun variable.

You will also need to create two ServiceInstaller class instances in the SpadesInstaller class just like we did above. When you are done, the Main method and the SpadesInstaller files should look like this:

static void Main(string[] args)
    // we'll go ahead and create an array so that
    // we can add the different services that
    // we'll create over time.
    ServiceBase[]    servicesToRun;

    // to create a new instance of a new service,
    // just add it to the list of services
    // specified in the ServiceBase array constructor.
    servicesToRun = new ServiceBase[]{
                    new SpadesAdminService(),
                    new SpadesLoginService(),
                    new SpadesGameService(),
                    new SpadesLobbyService() };

    // now run all the service that we have created.
    // This doesn't actually cause the services
    // to run but it registers the services with the
    // Service Control Manager so that it can
    // when you start the service the SCM will
    // call the OnStart method of the service.
    ServiceBase.Run( servicesToRun );

And the SpadesInstaller class.

public SpadesInstaller()
    ServiceProcessInstaller process =
             new ServiceProcessInstaller();

    process.Account = ServiceAccount.LocalSystem;

    ServiceInstaller serviceAdmin = new ServiceInstaller();

    serviceAdmin.StartType    = ServiceStartMode.Manual;
    serviceAdmin.ServiceName    = "SpadesAdminSvc";
    serviceAdmin.DisplayName    = "Spades Administration Service";

    ServiceInstaller serviceLogin = new ServiceInstaller();

    serviceLogin.StartType    = ServiceStartMode.Manual;
    serviceLogin.ServiceName    = "SpadesLoginSvc";
    serviceLogin.DisplayName    = "Spades Login Service";
    serviceLogin.ServicesDependedOn =
                new string[] { "SpadesAdminSvc" };

    ServiceInstaller serviceGame = new ServiceInstaller();

    serviceGame.StartType    = ServiceStartMode.Manual;
    serviceGame.ServiceName    = "SpadesGameSvc";
    serviceGame.DisplayName    = "Spades Game Service";
    serviceGame.ServicesDependedOn =
                new string[] { "SpadesAdminSvc" };

    ServiceInstaller serviceLobby = new ServiceInstaller();

    serviceLobby.StartType    = ServiceStartMode.Manual;
    serviceLobby.ServiceName    = "SpadesLobbySvc";
    serviceLobby.DisplayName    = "Spades Lobby Service";
    serviceLobby.ServicesDependedOn =
                new string[] { "SpadesAdminSvc" };

    // Microsoft didn't add the ability to add a
    // description for the services we are going to install
    // To work around this we'll have to add the
    // information directly to the registry but I'll leave
    // this exercise for later.

    // now just add the installers that we created to
    // our parents container, the documentation
    // states that there is not any order that you need
    // to worry about here but I'll still
    // go ahead and add them in the order that makes sense.
    Installers.Add( process );
    Installers.Add( serviceAdmin );
    Installers.Add( serviceLogin );
    Installers.Add( serviceGame );
    Installers.Add( serviceLobby );

In my next article, we're going to add support for having our own custom event log so that we can log our messages to it, but to take advantage of it we will also need to make sure it's installed. So we'll have to create an installer for it. I ran into problems doing this and will go into detail on how I had to solve the problem.


  • 1.0 - 10 Apr 2003
    • First release version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Terry Denham
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionhow to create account using c# Pin
Mohammamd Ali5-Jul-09 18:12
memberMohammamd Ali5-Jul-09 18:12 
Generalcreating multithreads in C# Pin
alzrrog11-May-07 11:02
memberalzrrog11-May-07 11:02 
GeneralVery useful Pin
Vicent Son22-Oct-06 15:23
memberVicent Son22-Oct-06 15:23 
GeneralGreate article, easy to follow, well done! Pin
ChristopherAtCodeProject14-Mar-06 15:04
memberChristopherAtCodeProject14-Mar-06 15:04 
GeneralA Complete Book Pin
sanalnaduvath1-Feb-06 19:46
membersanalnaduvath1-Feb-06 19:46 
GeneralRe: A Complete Book Pin
rizwan.afsar7-Apr-06 8:05
memberrizwan.afsar7-Apr-06 8:05 
Binoy Patel26-Oct-04 19:10
memberBinoy Patel26-Oct-04 19:10 
Member 1841425-Nov-10 1:34
memberMember 1841425-Nov-10 1:34 
QuestionReally nice article+samples/but why? Pin
tlongman9-Oct-04 8:35
membertlongman9-Oct-04 8:35 
AnswerRe: Really nice article+samples/but why? Pin
Terry Denham13-Oct-04 6:37
memberTerry Denham13-Oct-04 6:37 
GeneralGreat Article !!! Pin
Mehdi Moshtaghi6-Jul-04 19:53
memberMehdi Moshtaghi6-Jul-04 19:53 
GeneralProblem with Multiple Installers Pin
CBoland30-Jan-04 4:09
memberCBoland30-Jan-04 4:09 
GeneralExcellent article Pin
Per Søderlind26-Oct-03 12:14
sussPer Søderlind26-Oct-03 12:14 
GeneralQuestion Pin
nlgarvey25-Apr-03 10:12
membernlgarvey25-Apr-03 10:12 
GeneralRe: Question Pin
Ray Cassick25-Apr-03 10:25
memberRay Cassick25-Apr-03 10:25 
GeneralRe: Question Pin
Terry Denham25-Apr-03 13:19
memberTerry Denham25-Apr-03 13:19 
GeneralRe: Question Pin
Anonymous14-Oct-03 6:19
sussAnonymous14-Oct-03 6:19 
GeneralRe: Question Pin
Terry Denham13-Oct-04 6:40
memberTerry Denham13-Oct-04 6:40 
GeneralRe: Question Pin
casualinfoguy6-Jun-05 10:39
membercasualinfoguy6-Jun-05 10:39 
GeneralRe: Question Pin
Member 246301711-May-08 15:55
memberMember 246301711-May-08 15:55 
GeneralRe: Question Pin
CBoland30-Jan-04 3:55
memberCBoland30-Jan-04 3:55 
GeneralRe: Question Pin
mykone15-Feb-06 11:49
membermykone15-Feb-06 11:49 
GeneralGreat ! Pin
GriffonRL11-Apr-03 0:57
memberGriffonRL11-Apr-03 0:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180322.1 | Last Updated 10 Apr 2003
Article Copyright 2003 by Terry Denham
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid