VB .NET Services
Visual Basic .NET gives you the power to create Windows services.
September 3, 2001
I'm a VB guy who's never written a Windows service until recently. In the past, only C++ programmers had the pleasure of creating Windows NT services. But by using Visual Basic .NET (VB .NET), I now have the power and ability to create a Windows service, so I don't have to rely on C++ developers.
A great way to use such a service is to program the service to periodically pull data for new reports. For example, let's say you use a Web-usage analysis application that lets you choose from different Web sites, Web farms, Active Directories, or a combination of sources to build report definitions that you store in a database. These definitions contain detailed information such as Web site usage, page hits, and user history. The VB .NET service checks the database every minute or so for report requests, then calls the code that generates the report.
Here's a quick demonstration of how to set up a Windows service. In VB, select File, New, and Project to open a new project. Select the Windows Service template. Click on the link "Switch to code view." (I must admit that the first couple of times I used VB .NET, I was thrown off by all the code that it automatically generates.) You can change the code line "public class service1" to reflect the type of service you're creating. If you change the name here you'll need to change it in two other places. Click on the plus to the left of Component Designer generated code and replace the two instances of service1 with the name of your service.
Let's say that this service will run code that does system analysis every 3 minutes and writes the system status to a file. After you've made the preceding changes, drag a timer control onto the designer page. Now look at the timer's properties, and set the interval to the desired time. Note that the interval is in milliseconds, so if you want the service to run every 3 minutes, you need to set it to 180000. VB .NET also generates two sub routines called OnStart and OnStop. You use OnStart for code that makes the database check for new report requests and act appropriately. You use OnStop for tasks such as writing to a file to specify that the service stopped.
Next, you need to add an Installer. Switch to the Designer view, right-click, and choose Add Installer. You'll see a new window called ProjectInstaller.vb and two new components in the Designer; click ServiceProcessInstaller1 and look at the properties on the right. Make sure that you have selected the correct account for the service to run under. (This detail threw me for about an hour.) You also need to click on the component ServiceInstaller1 and make sure that the service name is the one you gave your service. Now build the service by selecting Build from the menu. You're almost done.
Last, you need to install the service by running the .NET Framework utility called installutil.exe. You can run this utility from the command prompt and type in the full directory path for your service. Or, you can click Start, Run, then type "installutil". Use Explorer to find the file, drag the file into the Run text field, then click OK. Your service is installed. If you click OK and nothing happens, you can go to the command prompt and type in "installutil /?". If you get nothing in response, you probably have more than one version of the utility on your system. Do a search on "installutil.exe" to find out whether your machine contains more than one version of the utility. If so, you probably have more than one copy of the .NET Framework on your machine. But if all is well, you can go to the Service Manager, find your service, and start it up.
I'm a VB guy who's never written a Windows service until recently. In the past, only C++ programmers had the pleasure of creating Windows NT services. But by using Visual Basic .NET (VB .NET), I now have the power and ability to create a Windows service, so I don't have to rely on C++ developers.
A great way to use such a service is to program the service to periodically pull data for new reports. For example, let's say you use a Web-usage analysis application that lets you choose from different Web sites, Web farms, Active Directories, or a combination of sources to build report definitions that you store in a database. These definitions contain detailed information such as Web site usage, page hits, and user history. The VB .NET service checks the database every minute or so for report requests, then calls the code that generates the report.
Here's a quick demonstration of how to set up a Windows service. In VB, select File, New, and Project to open a new project. Select the Windows Service template. Click on the link "Switch to code view." (I must admit that the first couple of times I used VB .NET, I was thrown off by all the code that it automatically generates.) You can change the code line "public class service1" to reflect the type of service you're creating. If you change the name here you'll need to change it in two other places. Click on the plus to the left of Component Designer generated code and replace the two instances of service1 with the name of your service.
Let's say that this service will run code that does system analysis every 3 minutes and writes the system status to a file. After you've made the preceding changes, drag a timer control onto the designer page. Now look at the timer's properties, and set the interval to the desired time. Note that the interval is in milliseconds, so if you want the service to run every 3 minutes, you need to set it to 180000. VB .NET also generates two sub routines called OnStart and OnStop. You use OnStart for code that makes the database check for new report requests and act appropriately. You use OnStop for tasks such as writing to a file to specify that the service stopped.
Next, you need to add an Installer. Switch to the Designer view, right-click, and choose Add Installer. You'll see a new window called ProjectInstaller.vb and two new components in the Designer; click ServiceProcessInstaller1 and look at the properties on the right. Make sure that you have selected the correct account for the service to run under. (This detail threw me for about an hour.) You also need to click on the component ServiceInstaller1 and make sure that the service name is the one you gave your service. Now build the service by selecting Build from the menu. You're almost done.
Last, you need to install the service by running the .NET Framework utility called installutil.exe. You can run this utility from the command prompt and type in the full directory path for your service. Or, you can click Start, Run, then type "installutil". Use Explorer to find the file, drag the file into the Run text field, then click OK. Your service is installed. If you click OK and nothing happens, you can go to the command prompt and type in "installutil /?". If you get nothing in response, you probably have more than one version of the utility on your system. Do a search on "installutil.exe" to find out whether your machine contains more than one version of the utility. If so, you probably have more than one copy of the .NET Framework on your machine. But if all is well, you can go to the Service Manager, find your service, and start it up.
About the Author
You May Also Like