Friday, July 2, 2010

Installing a Windows Service

There are already numerous posts on the web that explain how to do parts of this.  Unfortunately, few of them have all of the steps in one post.  Windows services are great, but they ain't all that easy to install once you are done developing them.

The following assumes Visual Studio 2008.

To create a windows service:

Start a New VB Project

Select Windows






    Select Windows Service









    The once it's created, double click the service1.vb file in the project explorer (or whatever you want to name it).

    There are only two sub routines in this file, OnStart() and OnStop().  There are comments inside each subroutine telling you how they should be used.

    In order to debug this service, you need to add the following sub routine to the file:

    Public Sub New()

    ' This call is required by the Windows Form Designer.

         InitializeComponent()

         ' Add any initialization after the InitializeComponent() call.

         If Debugger.IsAttached = True Then

              Dim tempargs() As String

              Me.OnStart(tempargs)

              System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)

         End If

    End Sub


    When you debug your service, you’ll actually be able to debug it like a regular console/winforms application.  You can leave this code in the application when you release it, since it won’t run if there is no debugger attached.

    Now comes the fun part.  Services don’t install the regular way:  Once you are done with your project, you need to add an Installer to your windows service project  Open the designer for the service class.  It will look something like this:








    Right click somewhere in the gray area and select Add Installer:
















    Give it a NAME!  Don’t leave it as Installer1.vb.  Call it Install.vb or something like that.  Double click your new install file (it’s in the solution explorer) and look at the designer view.  You should see a message stating that you should add components.  Right click the tool box and select “Choose Items”

    Wait a while so that VS can come up with the list of components.  This make take a bit.  There’s no progress bar so you’ll just have to wait.

    Once the list comes up, scroll down to the S’es and select ServiceInstaller and ServiceProcessInstaller.  Click okay and you should see them in your tool box.


    1. Drag a ServiceInstaller to the Designer
    2. Drag a ServiceProcessInstaller to the Designer
    3. Click on the ServiceProcessInstaller1 control
      1. Make sure the Parent Property is set to the name of your Installer Class (It’s the installer file you added to the solution explorer).  If you click Parent you should see a drop down list of classes.  Choose the installer class you added.
    4. Click the ServiceInstaller1 control
    5. Make sure the Parent Property is set to the name of your Installer Class (It’s the installer file you added to the solution explorer).  If you click Parent you should see a drop down list of classes.  Choose the installer class you added.
    6. Type the description of the service. (This will show up in the Services MMC window with all the other installed services).
    7. Type the DisplayName. (This will show up in the Services MMC window with all the other installed services).
    8. Type the ServiceName. (I believe this is the name of the process that will show up in Task Manager)
    9. Set the Startup Type (Automatic, Manual or Disabled)  You’ll probably want Automatic.
    10. Change the compile option from Debug to Release.
    11. Now you are ready to compile and install.
    12. Build the application.  Do no publish it.
      1. Make sure that the server you will run this on has .NET 3.5 SP1.  If it does not, you’ll have to install it on that server before continuing.
    13. Copy your release directory to that server.
    14. RDP to that server and open a command prompt.
    15. Change directory to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
    16. That is not a mistake, it should be the v2 directory.
    17. Run the following command:
    18. Installutil.exe (followed by the path to the exe you setup in step 13).
    19. If you selected the service to run as a User you’ll get a pop up asking for the user credentials.
    If everything installed correctly, you should see a bunch of text that ends with this:





    Open up the Services MMC and you should see your service installed.

    Be sure to start it.

    There you go.  Now you can run windows services instead of constantly writing console applications that are fired off every five minutes from the Windows Task Scheduler.

    No comments:

    Post a Comment

    Post a Comment