Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 48 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
48
Dung lượng
554,28 KB
Nội dung
2 DevelopingaWindowsService CERTIFICATION OBJECTIVES 2.01 Creating and Manipulating aWindowsService 2.02 Writing Code That Is Executed When aWindowsService Is Started or Stopped ✓ Two-Minute Drill Q&A Self Test CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:02 AM Color profile: Generic CMYK printer profile Composite Default screen CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 I n this chapter, you will learn about the Windows Services, previously known as NT Services. AWindowsservice is any application that needs to execute for long periods of time in its own session on a server. AWindowsservice starts without any intervention from a user when the server’s operating system boots, and it can authenticate using either the local SYSTEM account or a domain user’s account; in this way the Windowsservice can use the security context that best fits its purpose. The Windows Services also include software applications that have no interaction with the screen on the server where they are executed, such as an e-mail service or a web service. Note that at this time, support for Windowsdeveloping Services is available only in the Enterprise Edition. Support is not available in the Standard Edition of Visual Studio .NET. The examples in this chapter are written using the Enterprise Edition of Visual Studio .NET. CERTIFICATION OBJECTIVE 2.01 Windows Services The Windows Services technology is one of the important building blocks that you will use when building applications that must always run on the server, and that perform services for many users. Examples of Windows services are applications such as Internet Information Services (IIS), the Tardis clock service, and Microsoft Simple Mail Transport Protocol (SMTP). These services all run in the background, perform services for potentially many users or systems, and have no user interface. Windows services are managed through management applications that are specifically written to interact with them, or you can use the Services application that is available from the operating system. I have used the term “server” to refer to the computer where aWindowsservice is running, but that is not the whole story. The Windows NT and Windows 2000 families of operating systems, as well as Windows XP Professional, all can run Windows Services. However, Windows 9x, Windows Me, and Windows XP Home 2 Chapter 2: DevelopingaWindowsService CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:02 AM Color profile: Generic CMYK printer profile Composite Default screen CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 edition do not have the ability to run the Windows Services as part of the operating system. In these operating systems, aWindowsservice is executed as a user process that requires a user to be logged on to the computer. Windows services are used to extend the operating system; they are similar to daemons in Unix. Take a look at the Services application and what you can control in the services running on a server. If you are running Windows 2000 or Windows XP Professional, you access the Services application from the Run | Programs | Administrative Tools menu. If the Administrative Tools menu is missing in your Windows 2000 or XP Professional operating system, you will need to make it available by selecting Run | Settings | Task Bar & Start Menu. After you select Services in the Administrative Tools menu (shown in the following illustration), you will see a list of services that are installed on your server along with the current status of each service (shown in Figure 2-01), Windows Services 3 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:02 AM Color profile: Generic CMYK printer profile Composite Default screen 4 Chapter 2: DevelopingaWindowsService CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 indicating whether it is stopped, paused, running, or disabled. You can also see (from the startup type) if the service will automatically start with the operating system, or if it must be manually started. The listing of Windows Services is all-inclusive, meaning that all the known information about each service is displayed in the Services application. Further, the FIGURE 2-1 The Services application P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:03 AM Color profile: Generic CMYK printer profile Composite Default screen Windows Services 5 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 Description field is sometimes very long and contains the long form of the service’s name. In order to control the properties of aWindows service, you double-click the service in the Services application. Here, you can see the properties of the World Wide Web Publishing service, which is the name of the Web server part of IIS: As you can see, the properties that can be set on the General tab include the display name of the service as well as the description. You can also set the startup property to inform the operating system what type of startup you wish to have. The options are: ■ Automatic The service will be started as part of the operating system’s boot sequence. ■ Manual The service is enabled and will be started by the administrator (or by another service that requires it) when needed. ■ Disabled The service will never be started unless the startup property is changed. Set aservice to Disabled if you want to be sure that it never starts automatically, but set it to Manual if you want the service to start if another service depends on it. CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:03 AM Color profile: Generic CMYK printer profile Composite Default screen 6 Chapter 2: DevelopingaWindowsService CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 In the Service Status area, you will see the current state of the service as well as the controls to Start, Pause, Stop, or Resume the service. The Start Parameters field is used to pass parameters to the service when it starts. The Log On pane defines the account that will be used by the service to establish a security context. This service is not started when a user logs in to the server; rather, the service must authenticate to the server in order to get access to resources like memory, the processor, and the network. The default security account is the local SYSTEM account, which is the highest-permission account on any Windows server. Using the SYSTEM account is a very good idea as long as the service does not have to access resources over the network. Because the SYSTEM account has such powerful permissions, it is limited to the local system only, meaning that aservice cannot access any remote resources using it. If the service requires access to remote resources, you should use a domain user’s account to start the service. You can also associate the service with a specific hardware profile if specific settings are needed for the service. P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:03 AM Color profile: Generic CMYK printer profile Composite Default screen Windows Services 7 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 If the Windowsservice needs to access remote resources, you need to associate it with a user account because the SYSTEM account cannot be used to access remote resources. The Recovery pane gives you control over what action to take if the service fails. The settings are for the first and second failures as well as any subsequent failures. The Dependencies pane enables you to view the services that depend on your services as well as the services your service depends on. The system checks dependencies to make sure your service doesn’t start unless some other service has started first. P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:03 AM Color profile: Generic CMYK printer profile Composite Default screen CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 8 Chapter 2: DevelopingaWindowsService CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 When you start building Windows services, the first question that usually pops up is: What can you use service for? The answer is actually simple—anything! Okay, I hear you, that is an unfair answer, but it is true. Some of the most exciting aspects of the Windows operating system have been implemented as Windows services. For example, IIS is implemented as a service, as is Microsoft SQL Server and Microsoft Exchange Server. These are large and powerful applications, but they are implemented as services so that they can act together with the operating system, with no user intervention at all. I usually recommend to my students that they begin by building services that perform some small but interesting service for them. The following are some Windows services that have been built in just a week’s time: a time server that synchronizes the computer with a Network Time Protocol (NTP) server, a card deck service that returns a random card, a simple web server .the list goes on. Use the information in this chapter to build software that moves you and your projects forward. FROM THE CLASSROOM P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:03 AM Color profile: Generic CMYK printer profile Composite Default screen CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 The majority of these settings are controlled through the installation process. I will spend more time on this topic later in this chapter. Now that you have seen the Services application, proceed to build a small service to look at the mechanics of building and installing a service. CERTIFICATION OBJECTIVE 2.02 DevelopingaWindowsService In this section, you are going to build aWindowsservice that logs messages in the event log as its only function. We’ll set this limitation for now so that you can look at the different aspects of the service. In order to build aWindows service, you will need to have a minimum of the Professional Edition of Visual Basic .NET (the Standard Edition does not support Windows Services development). EXERCISE 2-1 The WindowsService Skeleton 1. Create a new project in Visual Studio .NET. Make it a Visual Basic .NET project and select WindowsService from the Template list as shown here. Give the project the name ServiceOne and save it to the C:\VB directory. DevelopingaWindowsService 9 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:03 AM Color profile: Generic CMYK printer profile Composite Default screen 2. Once the project is created, you should change the name of the Windowsservice to the name you have decided on, in our case, ServiceOne. To change the name, select the service in the Property Explorer and change the (name) and ServiceName properties as shown here: 3. Right-click the project in the Solution Explorer and select Properties in the context menu. In the Project Properties dialog, change the Startup Object to ServiceOne and click OK. This step tells the compiler what class contains the startup code; if you don’t change this property, the project will not compile but will report a missing Sub Main(). 4. Click “click here to switch to code view” in Design view to open the code module. Note that as you changed the name of the Windows service, Visual Studio .NET adjusted the names in the source code. 10 Chapter 2: DevelopingaWindowsService CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 2 P:\010Comp\CertPrs8\653-6\ch02.vp Wednesday, October 30, 2002 9:47:04 AM Color profile: Generic CMYK printer profile Composite Default screen [...]... Visual Basic NET / Lind / 222653-6 / Chapter 2 Windows Services Architecture 33 CERTIFICATION OBJECTIVE 2.03 Windows Services Architecture The Windows services that you built in the preceding section follow the same architecture as all Windows services regardless of the language they are built in Table 2-1 enumerates the three parts of all service architectures What makes the Windowsservice fairly easy... the Services application for all configurations, rather than working directly in the Registry for the reasons just stated WindowsService Control You have seen that the Services application that you use to configure aWindowsservice can also be used to control the state of the Windowsservice There are additional service control programs that can be used to perform the same types of actions on Windows. .. addition to the ServiceBase class that is implemented to produce the service program, there is the ServiceController class that is implemented to build the service controller program that sends messages to the service, as well as the ServiceProcessInstaller class that is implemented together with the ServiceInstaller class to create the installation program WindowsService Configuration AWindows service. .. Chapter 2 Developing a Windows Service You will now look at the service you have built, and what the output is so far When the service is installed, it will not start automatically; the automatic start happens only when the computer is rebooted To view and modify the state of the Windows service, you will have to open the Services application 33 Start the Services application from the Start | Settings... program The actual program that performs the work of the Windows service, this is what you developed in Exercise 2-3 The service program defines what actions to take when the Windowsservice is started, stopped, paused, or continued ServiceBase Service control program The service control program is in most cases the Services application from Application Tools This is the component that controls the state... Visual Basic NET / Lind / 222653-6 / Chapter 2 Developing a Windows Service The three parts of the Windowsservice architecture are implemented using inheritance from the three base classes—ServiceBase, ServiceController, and ServiceProcessInstaller The System.ServiceProcess namespace includes the classes needed to implement the three components of the WindowsService Architecture as can be shown in Table... DefaultMCAD/MCSD XML Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 2 Windows Services Architecture by navigating through the editor as shown here: Expand the Services key and locate the PingService key as shown next: Through this key, you can configure the Windowsservice and set, among other things, the startup property (Disabled, Manual, or Automatic)... the Windowsservice perform a ping to a remote host using a configurable delay The exercise makes use of a timer and the socket classes from the NET Framework 1 Create a new Visual Basic NET project, and make sure it is based on the WindowsService template The project should be named PingService and placed in the C:\VB folder Follow the steps from Exercise 2-1 to rename the service to PingService and... Windows services The net.exe utility has been part of virtually all Microsoft operating systems that have a network component Using the net.exe command-line utility, you can enumerate the currently running services (net start), start a service (net start pingservice) or stop a service (net stop pingservice), as can be seen in the following segment: C:\>net start These Windows 2000 services are started: Alerter... to make sure that the object that is instantiated is the class you are building—the Visual Studio NET editor does not correctly update the name when you change the name of the class Visual Studio NET does not change the class name that is instantiated in the Main() method You are going to give this service some low-level functionality that will record events in an event log The Windowsservice has predefined . and have no user interface. Windows services are managed through management applications that are specifically written to interact with them, or you can. 2 Developing a Windows Service CERTIFICATION OBJECTIVES 2.01 Creating and Manipulating a Windows Service 2.02 Writing Code That Is Executed When a Windows