Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Microsoft Windows 2000 ?????, Microsoft Windows XP Professional, ?? Microsoft .NET Framework ?? ??? Microsoft Windows XP ?????
Microsoft Visual Studio .NET ??????????? ???????, Visual Studio .NET ?????????? ?????????? ??????? ?? Microsoft Visual Studio 2005
?? ???? ????? ?? ?? ???? ??? Windows ?????? ?? ??? ?? ??????? familiarity ???? If you are not familiar with Windows Services, see the
first reference in the "??????"
section.
This article also assumes that the user account that you use
to install and to run this service has the required permissions to install and
to start services. The user account must also have the required permissions to access the event
log.
Create a Setup project for a Windows Service
This section describes how to create a Windows Service project
and how to use a compiled Setup project to install the Windows
Service.
Create a Windows Service project
????? ????,????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio .NET??,Microsoft Visual Studio 2005?? ????-????? ????, ?? ???? ???Microsoft Visual Studio .NET??,Microsoft Visual Studio 2005.
?????OnStartevent handler, replace the comments with the following code.
EventLog.WriteEntry("My simple service started.")
?????? Explorer, ??? ??? ????? ????Service1.vb.
????????????? ????? ???, ????? ????Add Installer.
????????dialog box for ServiceInstaller1, change theServiceName???? ?? ??? ???LogWriterService.
In Design view, clickServiceProcessInstaller1in the Code Editor.
????????????? ????? ???, ????????? ???????????? ?? ??? ???LocalSystem. TheLocalServicevalue and theNetworkServicevalue are only available in Microsoft Windows XP and later operating systems.
Use a compiled Setup project to
install the Windows Service
After you complete the steps in the "Create a Windows Service project" section
to configure the Windows Service project, you can add a
deployment project that packages the service application so that the service
application can be installed. ??? ???? ?? ???, ????? ????? ?? ???? ????::
Add a new project to your LogWriterService project.
To build the whole solution at the same time, clickConfiguration Manager????? ????????????? ?? ?? ??? ???? ?? ??? ???? ?????????? ??? ??? ?????ServiceSetup.
Press CTRL+SHIFT+B to build the
whole solution. When the solution is built, you have a complete Setup
package for the service.
To install the service, right-clickServiceSetup?? ????-????? ????, ?? ???? ?????????? ????.
?????ServiceSetup????? ????? ???, ????? ????next??? ???? Notice that a progress bar appears while the Setup program is installing the service.
When the service is installed, click??? ????.
Complete code listing (Service1.vb)
Public Class Service1
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.
EventLog.WriteEntry("Mysimple service started")
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
End Class
(service1.Designer.vb)
Imports System.ServiceProcess
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Service1
Inherits System.ServiceProcess.ServiceBase
'UserService overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
End Class
To verify that an event is
logged in the event log, use one of the following methods:
????? ????,????????? ????? ????,???????? ?????? ????? ????,???????????? ??????? ????-????? ????, ?? ???? ???????? ??????. ???? ??? ??? ????? ????Application Log. In the right pane, locate the event log for your service.
In Server Explorer, expand?????, ??????? ????ComputerName, ??????? ????Event Logs, ??????? ?????????????, ?? ???? ??? ??????? ????Service1. Remember thatService1is the name of the class and not the name of the service. Therefore,Service1is used as the application name. (It is beyond the scope of this
article to explain how to customize the names.) Move the cursor through the log
entries. The second entry from the top is the following: