devxlogo

Creating Windows Services in .NET

Creating Windows Services in .NET

uppose that your company’s field reps regularly upload files to the server, and the proper people need to be notified when these files arrive. Perhaps you need to monitor and log the creation of new folders, to ensure that certain files remain read-only, or to gather use data for reports. These and many other similar tasks are certainly within the capabilities of a standard Windows desktop application, and this is the approach that many developers and system administrators take, either running the application continuously or at regular intervals with the help of the Windows Scheduler. While common, this approach is a pretty sure invitation to headache-city. Being desktop apps, such programs are susceptible to intentional or unintentional fiddling from users. In addition, they require some account to be logged on in order to run.

Fortunately there’s a much better alternative for applications that run for extended periods and do not require user interaction: a Windows service. While Windows service applications (previously called NT Services) have been around for a while, they were a real bear to program. With .NET, this has changed. The .NET Framework provides classes that greatly simplify the tasks of creating, installing, and controlling Windows service applications.

While Windows service applications (previously called NT Services) have been around for a while, they were a real bear to program.

A Windows service is a highly specialized type of application designed to run for extended periods in its own Windows session, usually with no user interface. Tasks for which you might use a Windows service include network connection management, disk access monitoring, and certain security tasks. Windows itself always has a variety of services running, most of which the user is never directly aware of. In Windows 2000 you can view a list of services by opening Administrative Tools from the Control Panel, and then opening Computer Management and selecting Services on the navigation tree. The ability to create and deploy Windows services is not something that developers need to do on a regular basis, but in some situations it is by far the best solution.

The .NET Framework’s System.ServiceProcess.ServiceBase class encapsulates essentially all of the “plumbing” required to create and control service applications. By basing your application on this class, most of the difficult work is already done for you. Some versions of Visual Studio .NET (VS.NET) provide a Windows Service application template that creates the skeleton of your application. Note that Visual Basic Standard Edition and Visual C# Standard Edition do not include the Windows Service application template, so you’ll have to start from scratch.

At a minimum , you must add code to two methods in your service class. The Main() method is the entry point for the service and handles instantiating and running the service. Assuming a service named MyService, the code would look like this:

   Shared Sub Main()        Dim ServicesToRun() As _        System.ServiceProcess.ServiceBase     ServicesToRun = New _        System.ServiceProcess.ServiceBase() _        {New MyService()}     System.ServiceProcess.ServiceBase.Run _        (ServicesToRun)      End Sub 

The other method you must code is OnStart(), which executes when the service is started. Code in this method sets your service in motion so it can perform the tasks for which it is designed. For example, if your service monitors disk activity, your OnStart() method would instantiate one or more FileSystemWatcher objects.

Note that running a service and starting a service are two different things.

Note that running a service and starting a service are two different things. Running a service makes it available but does not start it. Once available, a service can be started or stopped either automatically, manually, or programmatically. Depending on the setting of the ServiceBase object’s CanPauseAndContinue property, a service may also be able to pause and continue.

Logging Information
Services typically have no user interface. One of the only ways that you can provide information about the service to administrators is by writing entries to a Windows event log. Thanks to the .NET Framework’s EventLog class, the procedure is quite simple. First, you instantiate an EventLog instance?typically in your service’s Main() method, providing the log name as an argument to the class constructor. You must also specify the Source property for the log?usually a string that identifies your application. For example:

   Dim MyLog As New EventLog(LogName)   MyLog.Source = SourceName  

Then, at other locations in your service, you call the WriteEntry() method to write information to the log. For example:

   MyLog.WriteEntry(LogMessage)  

You can use the Windows Event Viewer applet to view event logs, or you can use the EventLog class in an application to read existing logs. Be aware that creating and writing to a log consumes processor time and system resources, so this technique is best reserved for recording service-related events that are both important and occasional. You would not want to use an event log to record events that happen frequently, as would be the case with a service that monitors disk activity. In this type of situation it would be preferable to maintain information internally within the service and access it as needed..

Some services don’t need controlling; they start when the system boots and run until the system is shut down. You can set a service to start automatically either by setting the ProjectInstaller object’s StartType property or through the Services window of the Computer Management applet.

You can control a service using the ServiceController class. By including an instance of this class in your application (this must be a separate application from the Windows service application itself), you can:

  • Start or stop the service
  • Pause and continue the service (only if the service’s CanPauseAndContinue property is True)
  • Send custom commands to the service
  • Retrieve information from the service

There are three ways to add an instance of the ServiceController class to your project:

  • In the VS.NET Server Explorer, open the Servers node, open the desired server, then open the Services list. Drag the desired service onto your form.
  • Open the Components tab of the Toolbox and drag the ServiceController component onto your form. Set the component’s MachineName property to the name of the server where the service resides (use “.” for the local machine), and set the ServiceName property to the name of the service.
  • Create an instance of the ServiceController class in code, then set its MachineName and ServiceName properties as described above.

Because the Servers node of the Server Explorer is not available in Visual Basic Standard Edition or Visual C# Standard Edition, you must use the second or third technique to add a ServiceController component to your project.

After connecting your ServiceController component to the service, you can:

  • Use the ExecuteCommand() method to send a custom command to the service. The service responds to these commands in the OnCustomCommand() method.
  • Set the service’s CanPauseAndContinue, CanStop, and CanShutDown properties.
  • Use the Start(), Pause(), Continue(), and Stop(), methods to control the service.
  • Create a proxy through which you can communicate with and retrieve information from the service.

The controlling application can be a standard Windows application, or can display itself as an icon in the system tray from which the user can select commands and options. It could also be an ASP.NET application that permits a remote user to monitor and administer a service running on a remote system.

Unlike other types of .NET applications, you must explicitly install a Windows service application on the target system. Fortunately, VS.NET makes this task quite easy. For service projects, the IDE displays a link in the Properties window titled “Add Installer.” When you click the link VS.NET adds the necessary installer classes to your project, as part of a separate module. You can set various properties, the most important of which is the Account property of the ServiceProcessInstaller class. The Account property determines the security context under which the service will run.

The choices are:

  • LocalService: The service has extensive local privileges and presents the computer’s credentials to remote servers.
  • LocalSystem: The service has limited local privileges and presents anonymous credentials to remote servers.
  • NetworkService: The service has limited local privileges and presents the computer’s credentials to remote servers.
  • User: You specify a local or network account, and provide the username and password either as properties or during installation. The service uses the security context of the specified user account.

To install a service you must compile the service project into an exe file, and then run the utility program InstallUtil.exe (installed with the .NET Framework). At a command prompt enter the command:

   installutil servicename.exe 

You must provide the complete path to the service’s exe file. The InstallUtil application installs the specified service. will be installed. After installing the new service, you can find it in the Services window of the Computer Management applet. (You can use the InstallUtil program to uninstall a service as well.)

Windows services are an often overlooked type of application. They are relatively specialized, but for certain type of tasks, usually in the administrative category, they fit the bill perfectly. The .NET Framework provides classes designed specifically for creating and controlling Windows service applications. Given that the required programming is relatively simple, you should take advantage of Windows services.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist