Click here to Skip to main content
15,173,454 members
Articles / Programming Languages / Visual Basic
Posted 16 Aug 2007


70 bookmarked

Developing Windows Services using Visual Studio .NET Explained - Part 2

Rate me:
Please Sign up or sign in to vote.
4.26/5 (12 votes)
29 Jul 2008CPOL6 min read
Article explaining the step by step creation and installation of a simple windows service

If you haven't read part 1 of this article yet, click HERE to read it first.

Creating Sample Service

Bellow are the steps required to create the sample .NET Service used throughout the remainder of this article:
  1. Startup Visual Studio, and select the "Visual Basic" node under "Project Types". Under Templates, select "Windows Service" and name the project "MyWinService".
  2. Under Solution Explorer, select the file name "Service1.vb", and change it to "MyWinService.vb" under Properties section.
  3. Open "MyWinService.vb" in design mode. Right-Click on the design area and choose "Add Installers". This will place two components called "ServiceProcessInstaller1" and "ServiceInstaller1" onto the design area.
  4. Select "ServiceInstaller1", change its name to "MyWinServiceInstaller". Also change the ServiceName from "Service1" to "MyWinService".
  5. Select "ServiceProcessInstaller1", and change its name to "MyWinServiceProcessInstaller".

Important Settings

  • Service Installer: Select the ProjectInstaller.vb file from the solution explorer, and click on the MyWinServiceInstaller icon. The Service Installer has the following important properties:
    • Description: This is the description that shows under Windows Services, which basically describes your service to other users.
    • Service Name: This is the name through which Windows will identify your service.
    • Start Type: The options here are Manual, Automatic or Disabled. Automatic will mean that the service will start on its own every time windows starts. Manual means that you have to manually go to services and start it every time you need to use it. Disabled means the service will not be functional at all.
  • Service Process Installer: Click on MyWinServiceProcessInstaller icon under ProjectInstaller.vb file. The most important option here is the "Account" option. The choices here are:
    • LocalService: Built in account which is similar to authenticated local user. If you run the service under this account it will not have any network rights.
    • NetworkService: Built in account which is similar to authenticated network user. Running your service under this mode will avoid password expiration issues and is generally good for domain environments.
    • LocalSystem: This is a very powerful high priviledge account that is usually not desired for custom services (even though it makes running the service very easy, it could present security issues later).
    • User: This can either be a local machine user or a domain user. If you are planning on deploying your service to other servers on other domains you either have to make sure you use the correct username!
  • Service Behavior Options: The general service options determine how the service logs events, how and if it can pause, stop, or resume and so on. Here are the choices:
    • AutoLog: If you use this option, you are basically handing over the control of the Event Log Entry to the Service.
    • CanHandlePowerEvent: For laptop power events. Not needed for most services.
    • CanHandleSessionChangeEvent: Generally a good idea to set this one to True.
    • CanPauseAndContinue: Important if you want to allow users to pause and resume the service.
    • CanShutdown: Very good idea to set this one to True so you can actually shut down the service!
    • CanStop:Again, good idea to set this one to True so you can actually STOP your service!

The Code

By default, two Subroutines called "OnStart" and "OnStop" are created when you create a Windows Service. Here is what needs to be placed in each section of our Windows Service to get the sample service up and running:

Protected Overrides Sub OnStart(ByVal args() As String) 
EventLog.WriteEntry("MyService Started") 
End Sub 

Protected Overrides Sub OnStop() 
EventLog.WriteEntry("MyService Stopped") 
End Sub 

Protected Overrides Sub OnPause() 
EventLog.WriteEntry("MyService Paused") 
End Sub 

Protected Overrides Sub OnContinue() 
EventLog.WriteEntry("MyService Resumed") 
End Sub 

Protected Overrides Sub OnCustomCommand(ByVal command As Integer) 
If command = 200 Then 
EventLog.WriteEntry("Custom Command 200 invoked") 
ElseIf command = 210 Then 
EventLog.WriteEntry("Custom Command 210 invoked") 
End If 
End Sub 

To build the service, simply click on "Build > Build MyWinService". This will create an executable file either in ".\bin\Release\" or ".bin\Debug" depending on if you compile it in Debug or Release mode.

Installing Windows Service

There are two ways to install a windows service:

  1. Using "InstallUtil.exe" for the correct .NET Framework Version.
  2. By Creating a "Setup and Deployment" project for your service.

When developing the windows service, it is a lot easier to use the Install Utility to install and uninstall your windows service. You do however have to keep track of the version of this utility corresponding to your development framework. For Visual Studio 2005, the .NET Framework 2.0 version of this utility will need to be used.

If you have several versions of the Visual Studio installed on the same machine, and are actively developing using all of them, you may have the following 2 versions of this tool. Please note that depending on the subversion of the framework you have installed the actual version folder name may be different:



Here are the steps to follow in order to use this utility and install your service:

  • Start command prompt by go "Start > Run", typing "cmd" and pressing the "OK" button.
  • Navigate to the .NET Framework directory above that corresponds to your version of Visual Studio that you are using to develop the Windows Service (VS 2003 use 1.1, VS 2005 use 2.0). In my case that would be "c:" on command line followed by "cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727".
  • Invoke the Install Utility by typing InstallUtil "c:\[PATH TO YOUR SERVICE EXE]\MyWinService.exe" to register the windows service. Your screen should look something like this:

Screenshot - 081607_b1_i1_sm.gif

  • You will be prompted to enter a username and password for the service that looks like this. The username that you enter here can be a domain user, entered in the form "domain\username". Enter both your username and password and hit "OK" to run the utility:

    Screenshot - 081607_b1_i2.gif

  • Once the Utility is finished, you should see a screen output similar to the following:

    Screenshot - 081607_b1_i3_sm.gif

    The last step to running the service is to start it from the Service Control. To do this, navigate to "START > Control Panel > Administrative Tools > Services" and start the Service Controller Interface. Scroll down until you see the name "MyWinService". Select that service, Right-Click on it and choose "Properties".

    The two important Tabs that we care about are the "General" and "Log On" tabs. On the General Tab, you will see a combo box called "Startup type", with the options "Manual", "Automatic" and "Disabled". Manual option means that the service will not start when windows starts up, while Automatic means that the service DOES start up every time windows starts. Click on the Log On tab. Here you can change the user, under which the service runs. You should have the user you entered in the Install Utility showing on this screen at this point. If you would rather run the service under the local system user, you can select the "Local System Account" radio button. Click "Apply" to accept your changes, then click back on the General Tab. Start the service by clicking on the "Start" button on this tab (alternatively you can start the service directly from the Service Controller by right clicking on the name of it and choosing "Start" from the context menu provided).

    Event Log Entries

    By starting our custom made .NET Service, a new event log entry is made under the Application Log. To view the events of this log, go to "START > Control Panel > Administrative Tools > Event Viewer" and select the "Application" log section on the left menu of the Event Viewer.

    This concludes Part 2 of this article. More to come in Part 3.

    Pete Soheil
    DigiOz Multimedia

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    About the Author

    DigiOz Multimedia
    Software Developer (Senior) DigiOz Multimedia
    United States United States
    Pete Soheil is the Creator and Owner of DigiOz Multimedia, a Chicago-IL based company that specializes in the creation of Windows, Web and Mobile Application Development in a variety of Programming Languages, including C#, VB.NET, ASP.NET, ASP, VC++, GCC, PHP and Perl.

    Comments and Discussions

    QuestionService Not Running once the System Password Expires Pin
    Member 341188919-Nov-13 21:23
    MemberMember 341188919-Nov-13 21:23 
    AnswerRe: Service Not Running once the System Password Expires Pin
    DigiOz Multimedia16-Jan-14 4:54
    MemberDigiOz Multimedia16-Jan-14 4:54 
    GeneralIntractive Service Pin
    73amit10-Oct-07 20:50
    Member73amit10-Oct-07 20:50 
    GeneralRe: Intractive Service Pin
    DigiOz Multimedia10-Oct-07 21:54
    MemberDigiOz Multimedia10-Oct-07 21:54 
    GeneralService Type Pin
    73amit3-Oct-07 3:32
    Member73amit3-Oct-07 3:32 
    GeneralRe: Service Type Pin
    DigiOz Multimedia10-Oct-07 12:59
    MemberDigiOz Multimedia10-Oct-07 12:59 
    GeneralRe: Service Type Pin
    73amit10-Oct-07 20:06
    Member73amit10-Oct-07 20:06 
    GeneralUser definition Pin
    Carlos.Crespo2-Sep-07 9:51
    MemberCarlos.Crespo2-Sep-07 9:51 
    GeneralRe: User definition Pin
    DigiOz Multimedia2-Sep-07 17:59
    MemberDigiOz Multimedia2-Sep-07 17:59 
    GeneralTimer Issue Pin
    Carlos.Crespo2-Sep-07 9:36
    MemberCarlos.Crespo2-Sep-07 9:36 
    GeneralRe: Timer Issue Pin
    Carlos.Crespo2-Sep-07 10:35
    MemberCarlos.Crespo2-Sep-07 10:35 
    GeneralRe: Timer Issue [modified] Pin
    DigiOz Multimedia2-Sep-07 17:55
    MemberDigiOz Multimedia2-Sep-07 17:55 
    GeneralMultiples!! Pin
    Sam Heller22-Aug-07 7:35
    MemberSam Heller22-Aug-07 7:35 
    GeneralRe: Multiples!! Pin
    DigiOz Multimedia22-Aug-07 7:48
    MemberDigiOz Multimedia22-Aug-07 7:48 
    GeneralRe: Multiples!! Pin
    Sam Heller22-Aug-07 7:51
    MemberSam Heller22-Aug-07 7:51 
    GeneralRe: Multiples!! Pin
    DigiOz Multimedia23-Aug-07 5:17
    MemberDigiOz Multimedia23-Aug-07 5:17 
    GeneralRe: Multiples!! Pin
    Sam Heller23-Aug-07 5:56
    MemberSam Heller23-Aug-07 5:56 
    GeneralRe: Multiples!! Pin
    DigiOz Multimedia23-Aug-07 6:47
    MemberDigiOz Multimedia23-Aug-07 6:47 
    GeneralRe: Multiples!! Pin
    Sam Heller28-Aug-07 6:00
    MemberSam Heller28-Aug-07 6:00 
    GeneralRe: Multiples!! Pin
    DigiOz Multimedia28-Aug-07 8:05
    MemberDigiOz Multimedia28-Aug-07 8:05 
    GeneralRe: Multiples!! Pin
    Sam Heller10-Sep-07 6:16
    MemberSam Heller10-Sep-07 6:16 
    GeneralRe: Multiples!! Pin
    DigiOz Multimedia10-Sep-07 6:23
    MemberDigiOz Multimedia10-Sep-07 6:23 
    GeneralGreat help! Pin
    Ron4407721-Aug-07 14:16
    MemberRon4407721-Aug-07 14:16 
    GeneralRe: Great help! Pin
    DigiOz Multimedia21-Aug-07 15:59
    MemberDigiOz Multimedia21-Aug-07 15:59 
    Generalinstallutil Pin
    Brian Grant21-Aug-07 3:27
    MemberBrian Grant21-Aug-07 3:27 

    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.