Introduction to Windows Services

A Windows service is an application that runs on a server or workstation computer and provides ongoing functionality without direct user interaction. Windows services are often used to perform system monitoring.

A Windows service will run in its own process, independently of users or other programs running on the same computer. Windows services are frequently configured to start automatically when the computer boots up. Unlike most applications, Windows services run under their own security identity, rather than under the identity of the currently logged-in user. They can start running, even if there is no user logged onto the computer. This behavior is exactly what is needed for applications that run unattended on a server or that need to be available all the time on a desktop computer.

Note 

The Visual Studio project templates and associated functionality that enable you to create Windows service applications are not available in the Visual Basic .NET Standard Edition. Neither is the Server Explorer feature. These features are included with Visual Studio .NET Professional, Enterprise Developer, and Enterprise Architect Editions.

When you create an application that will run as a Windows service, you must be careful not to include any user interface elements such as message boxes or other dialog boxes. A Windows service is not meant to provide a visual interface for users.

A Windows service will typically report its results and error messages to an event log.

start sidebar
Real World Scenario-Using Windows Services to Monitor a Directory

You are a software developer for a medium-sized organization. You are hoping that some new features of the .NET Framework will help solve a problem that your department has been facing for some time. Your department is in charge of managing documents that are submitted for posting on your company's website. Documents are submitted by many departments throughout the company. End users simply copy the files to a designated network-shared directory.

Your department needs to know when new files are added to the directory. A consultant who left long ago wrote a system to periodically check the directory, but no one currently on the staff knows how to make changes or maintain the program.

A Windows service application is the perfect solution for this type of requirement. The service will always be running on the server, so administrators do not have to remember to check the directory or manually run a program. The .NET Framework even provides other useful classes, such as the FileSystemWatcher, which handles the actual task of firing events when files are added, deleted, or changed in the target directory. The Windows service can write to an event log, so there is an audit history.

By using the security features in the .NET Framework, your application can check the user's identity and permissions to make sure they are authorized to make changes. Other Framework classes provide the means to send an e-mail message, if necessary to notify administrators when new documents have been added.

It's clear that the .NET Framework provides a wealth of features to quickly and easily design solutions for this kind of common business requirement.

end sidebar

An administrator can interactively manage Windows service applications by using the Service Control Manager (see Figure 1.1). You can find this tool under different menus, depending on the operating system you are using:

  • Start Ø Programs Ø Administrative Tools Ø Services in Windows 2000 Server

  • Settings Ø Control Panel Ø Administrative Tools in Windows 2000 Professional

  • Start Ø Control Panel Ø Administrative Tools Ø Services in Windows XP Professional

    click to expand
    Figure 1.1: The Service Control Manager console

The Service Control Manager shows you a list of all services that are installed on the computer. For each service, you can see the name, description, current status (Started, Paused, or Stopped), startup type (Automatic-starts automatically on boot, or Manual) and the identity that the service logs on as. By using the menus and toolbar buttons, you can issue commands to start, stop, pause, continue, or restart the selected service. You can also view a Properties dialog box that enables you to change configuration options for a service.

Alternatively, you can view the Windows services running on your computer directly from within Visual Studio .NET by using the Server Explorer. To open the Server Explorer, choose View Ø Server Explorer. Expand the node Servers, expand the node with your computer name, and then expand the Services node. You will see all the services that are running. When you right-click on a service, the pop-up menu provides options to start or stop the service and view the properties. You see a bit less detail here than in the Service Control Manager, but it is convenient to be able to start and stop the service from within Visual Studio .NET.

In Exercise 1.1, you will use the Windows Service Control Manager utility to view the existing Windows services that are currently running on your computer.

Exercise 1.1: Using the Service Control Manager

start example
  1. Start the Service Control Manager. For Windows 2000 Server, choose Start Ø Programs Ø Administrative Tools Ø Services. (If you are using a different operating system, see the instructions provided earlier, immediately before Figure 1.1.).

  2. Review the list of services that are running on your computer. For instance, the CustomLogService entry.

  3. Right-click on the service and choose Start or Stop from the pop-up menu.

    click to expand

  4. The status of the service changes in the Service Control Manager window. It is important to note that you should right-click the service name again and return the service to its original state. You don't want to inadvertently cause another application that depends on this service to fail or, conversely, to leave an unnecessary service running.

  5. Right-click one more time and choose Properties. Review the choices that are available in the Properties dialog box.

end example



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

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