X
Business

Automate tasks with Windows Services (Part 2)

Find out how the .NET Framework's Windows Services (formerly known as NT services) allows you to program and schedule system tasks.
Written by Tony Patton, Contributor

In part one, I covered the inner workings of a Windows Service developed with the .NET Framework. This included a somewhat rudimentary Windows Service application with both VB.NET and C#. This week, we'll install and execute the Windows Service. (Note: This story contains the complete code for part one and part two.)

Windows service installation
While console, Windows Form, and ASP.NET applications are easy to install by copying files, you must explicitly install Windows Service applications. Visual Studio .NET simplifies the installation process for Windows server applications. It displays a link in the Properties window of the Windows Service called Add Installer. When you select the link, the IDE adds the necessary installer classes to your project as part of a separate module.

This includes two classes: System.ServiceProcess.ServiceProcessInstaller and System.ServiceProcess.ServiceInstaller. Several properties may be set using the Properties window of each class. The most important property is Account within the ServiceProcessInstaller class. It specifies the Windows account under which the service runs (security context). The following options are available:

  • LocalService: Service has extensive local privileges and presents the computer's credentials to remote servers.
  • LocalSystem: Service has limited local privileges and presents anonymous credentials to remote servers.
  • NetworkService: Service has limited local privileges and presents the computer's credentials to remote servers.
  • User: A local or network account is specified. You may specify the necessary username and password via properties, or you may type them during installation. The Service uses the security context of the specified user account.

When an installer is added to the Service, the ProjectInstaller class is added. Defining properties via each class's Properties window results in an associated line of code in the corresponding ProjectInstaller class. The following code shows a sample installer for our C#-based Windows service with various properties defined, including the Account set to LocalService and ServiceName set to BuilderService.

The corresponding VB.NET code follows:

With the installers added to the project, you may now install the Windows Service and run it on the target computer. Another approach within our project is how the service is started. There are three options:

  • Manual: The user starts the Service.
  • Disabled: The Service isn't available for use.
  • Automatic: The Service starts automatically when the host computer starts.

In our example, I have the Service start automatically. With the Service properly compiled, we install it on the target computer via the Microsoft .NET Framework Installation utility (InstallUtil.exe) command-line tool. It allows you to easily install and uninstall our service. The compiled file (executable) or assembly is passed to it. Also, it provides the following command-line switches:

  • /LogFile=[filename] - Contains the log indicating installation success/failure. The default is the AssemblyName.InstallLog.
  • /LogToConsole=(true|false) - Signals whether console output is enabled.
  • /ShowCallStack - Signals whether call stack is displayed if an exception is encountered.
  • /u - Uninstalls the Service.

With our sample Service, the following line takes care of the installation:

InstallUtil WindowsService.exe

The line is run in the directory containing the assembly; otherwise, it would require the complete path to the assembly. After you install the new Service, it's located in the Services window of the Computer Management applet.

Triggering execution
Once you properly install the Service, it's triggered based upon the Service setting. Unfortunately, it's only executed when loaded or run by a user. You may augment this by adding a timer to the code, so the agent executes on a scheduled basis. The next VB.NET code listing shows our Service with a Timer object added. The timer is started when the Service starts, and it stops when the Service stops.

Use the right tool
A Windows Service is an excellent application choice when working with administrative tasks or automating routine tasks. Add it to your toolbox and use it when the situation arises.

Editorial standards