Program and Employ Windows Services for Microsoft SharePoint 2007, Page 3
Figure 1: InstallUtil.exe tool in action
The tool will indicate the process for installing the service on screen and, if at the end, the following message appears: "The transacted install has completed", the installation procedure is completed. A log file is generated in the same directory of the EXE file together with a résumé of the process and the errors found, if any.
In the same way, the service can be uninstalled by using the syntax:
installUtil /u [path to the service compiled exe file]
The Windows Service can be found in the Services Manager application ("Administrative Tools" - "Services").
The Services Manager implements the functions start, stop, pause, and resume the working of the service; it defines the security context wherein the service will run (tab "Log On"); and the actions the service will take if a failure occurs (tab "Recovery"). A Windows Service runs in its own security context, which may be different from the context of the current user. There are four context types:
- LocalSystem Account is the Windows system administration account with widespread computer rights, giving the credentials of the account to any service that requests it
- LocalService Account provides fewer rights than the LocalSystem and gives anonymous credentials if requested
- NerworkService Account has the lowest level of rights, but give the credentials of the account if they are requested
- User Account specifies a single user in the system, whose rights are established using the Windows users accounts system
The service uses the credentials of its security context to read the information from SharePoint, so the account must have the appropriate rights inside the Portal. And, because the information to be read is width spread in SharePoint, the account needs to have Administrator rights in the Portal or the code require to use an Impersonator to be able to use the Object Model.
Debugging a Windows service is less straightforward than other Windows applications. To begin debugging, install and start the service before attaching the process to the Visual Studio debugger. The attachment process actually interrupts the service and a BreakPoint in Visual Studio may halt the service although technically the service is still running. After that the service begins, go to "Debug" in Visual Studio, "Processes", click "Show system processes", select the process of the service, and click "Attach". The normal debug function of Visual Studio then can be used.
The debugger is attached after the service starts; therefore, it is not possible to debug the "OnStart" or "Main" methods. This is done using a dummy service that loads the service process to be debugged. If you attach the dummy service to the debugger, it is possible to trace the initialization of the real service. The "OnStart" method has a limit of 30 seconds for each start attempt, so it is difficult to completely debug the code in the method, especially if the code is extensive or complex.
The general SharePoint user is unaware of the working of the service; however, server administrators are able to see when the service has been started/stopped in the EventLog entries and can monitor failures. Every 24 hours, a file with all the gathered information from the SharePoint Portal is generated by the service.
Figure 2: Output file from the service
Windows Services, although not a part of the SharePoint infrastructure, can play an important role in the Portal as well. Automated tasks that need to be executed independent of SharePoint can be programmed as Windows Services. The Service can use all the programming code from the DotNet FrameWork and the SharePoint Object Model.
About the Author
Gustavo Velez is a MCSD Senior Application Developer for Winvision (http://www.winvision.nl), a Microsoft Gold Partner in the Netherlands. He has many years of experience developing Windows and Office applications, and more than five years of daily programming experience with SharePoint. The author's articles can be found in many of the leading trade magazines in English, Dutch, and Spanish. He is also pleased to be Webmaster of http://www.gavd.net/servers, the only Spanish-language site dedicated to SharePoint. Spanish-language readers may want to consult Velez.s new book, Programacisn con SharePoint 2007 (http://www.dotnetmania.com/Libros/index.html).