Tài liệu Developing a Windows Service - Pdf 85

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 Service
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. A Windows service is any application that needs to execute for long periods of
time in its own session on a server. A Windows service 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 Windows service can use
the security context that best fits its purpose. The Windows Services also include software

Chapter 2
edition do not have the ability to run the Windows Services as part of the operating
system. In these operating systems, a Windows service 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: Developing a Windows Service
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.

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: Developing a Windows Service
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 a service 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

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

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: Developing a Windows Service
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
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 2
5.
The code that was created by the Windows Service Wizard is shown in the
following code listing:
Imports System.ServiceProcess
Public Class ServiceOne
Inherits System.ServiceProcess.ServiceBase
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerStepThrough()> _
Developing a Windows Service
11
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
Private Sub InitializeComponent()
'
'ServiceOne
'
Me.ServiceName = "ServiceOne"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop
' your service.
End Sub
End Class
The bolded text in the preceding listing is the one area that needs to be
modified after the service is constructed by Visual Studio .NET. You will
need 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.

following code segment shows how to open an event log:
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the
InitializeComponent() call
' Test if the event log exists; if not, create it
If (Not
System.Diagnostics.EventLog.SourceExists("ServiceOne")) Then
EventLog.CreateEventSource("ServiceOne",
"ServiceOneLog")
End If
' Set the source and log properties for the event log
EventLog1.Source = "ServiceOne"
EventLog1.Log = "ServiceOneLog"
End Sub
The event log is exposed to our Windows service through the System.Diagnostics
namespace. By verifying that the event log exists and creating it if it does not,
you are guaranteed that, when you associate the EventLog1 component that
was added to the service with our event log, the log will be there.
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 2
Developing a Windows Service
13
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

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
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 2
The result is shown in the following illustration, where you can see the
Design view of the new ProjectInstaller.vb file. You will notice that there are
two instances of the ProjectInstaller class: first the ServiceInstaller1 that will
install our Windows service and then the ServiceProcessInstaller1 that is used
to install the Windows service’s associated process.
12.
Change the name of the ServiceInstaller1 component to ServiceOneInstaller.
13.
Verify that the StartType property is set to Automatic.
Developing a Windows Service
15
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:05 AM
Color profile: Generic CMYK printer profile
Composite Default screen
16
Chapter 2: Developing a Windows Service
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 2
14.

P:\010Comp\CertPrs8\653-6\ch02.vp
Wednesday, October 30, 2002 9:47:05 AM
Color profile: Generic CMYK printer profile
Composite Default screen
18
Chapter 2: Developing a Windows Service
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 2
21.
The Add Project Output Group dialog box is displayed. Verify that the
ServiceOne Windows service is selected in the Project control and that
Primary Output is selected. Click OK.
22.
Now you need to add the directions that will install the Windows service.
Right-click the ServiceOneSetup project in the Solution Explorer and select
View | Custom Actions from the context menu as shown here:
P:\010Comp\CertPrs8\653-6\ch02.vp
Wednesday, October 30, 2002 9:47:06 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Developing a Windows Service
19
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 2
23.
The Custom Actions editor is displayed.
24.
Right-click Custom Actions in the Custom Action editor. Select Add
Custom Action… from the context menu to open the dialog to select the
items in the project.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status