Creating a Windows Service in Visual Basic


Now it’s time to create and use a Windows Service with Visual Basic, using the previously discussed .NET Framework classes. These tasks are demonstrated later in a detailed example. Here is a high-level description of the necessary tasks:

  1. Create a new project of the type Windows Service. By default, the service will be in a module named Service1.vb, but it can be renamed, like any other .NET module. (The class that is automatically placed in Service1.vb is named Service1 by default, and it inherits from ServiceBase.)

  2. Place any logic needed to run when the service is started in the OnStart event of the service class. You can find the code listing for the Service1.vb file by double-clicking this file’s design surface.

  3. Add any additional logic that the service needs to carry out its operation. Logic can be placed in the class for the service, or in any other class module in the project. Such logic is typically called via some event that is generated by the operating system and passed to the service, such as a file changing in a directory, or a timer tick.

  4. Add an installer to the project. This module provides the interface to the Windows operating system to install the module as a Windows Service. The installer is a class that inherits from System.Configuration.Install.Installer, and it contains instances of the ServiceProcessInstaller and ServiceInstaller classes.

  5. Set the properties of the installer modules as necessary. The most common settings needed are the account under which the service will run and the name the service will display in the Service Control Manager.

  6. Build the project. This results in an EXE file. For example, if the service were named WindowsService1, then the executable file would be named WindowsService1.exe.

  7. Install the Windows Service with a command-line utility named InstallUtil.exe. (As previously mentioned, a service cannot be started by just running the EXE file.)

  8. Start the Windows Service with the Service Control Manager (available via the Control Panel image from book Administrative Tools folder in Windows 2000, or the Start image from book All Programs image from book Administrative Tools folder in Windows XP) or with the Server Explorer in Visual Studio 2005.

    You can also start a service from the command console if the proper paths to .NET are set. The command is “NET START <servicename>”. Note that the <servicename> used in this command is the name of the service, not the name of the executable in which the service resides. Depending on the configuration of your system, a service started with any of the aforementioned methods will sometimes fail with an error message that says the service did not start in a timely fashion. This may be because the .NET libraries and other initialization tasks did not finish fast enough to suit the Service Control Manager. If this happens, attempt to start the service again; it usually succeeds the second time.

Tip 

Steps 2 through 5 can be done in a different order. It doesn’t matter whether the installer is added and configured before or after the logic that does the processing for the service is added.

At this point, a service is installed and running. The Service Manager or the Server Explorer can stop the service, or it will be automatically stopped when the system is shut down. The command to stop the service in a command console is “NET STOP <servicename>”.

The service does not automatically start the next time the system is booted unless it is configured for that. This can be done by setting the StartType property for the service to Automatic when developing the service or it can be done in the Service Manager. Right-clicking the service in the Service Manager provides access to this capability.

This process is superficially similar to doing most other Visual Basic projects. There are a few important differences, however:

  • You cannot debug the project in the environment as you normally would any other Visual Basic program. The service must be installed and started before it can be debugged. It is also necessary to attach to the process for the service to do debugging. Details about this are included in “Debugging the Service.”

  • Even though the result of the development is an EXE, you should not include any message boxes or other visual elements in the code. The Windows Service executable is more like a component library in that sense, and should not have a visual interface. If you include visual elements such as message boxes, the results can vary. In some cases, the UI code will have no effect. In others cases, the service may hang when attempting to write to the user interface.

  • Finally, be especially careful to handle all errors within the program. The program is not running in a user context, so a runtime error has no place to report itself visually. Handle all errors with structured exception handling, and use an Event Log or other offline means to record and communicate runtime errors.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net