Visual Basic .NET ?? Visual Basic 2005 ??? Windows ???? ?? ??? ?? ????? ????????? ?? ????? ?? ??? ???? ????

???? ID: 317421 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
?? Microsoft Visual C# .NET ??????? ?? ??? ?? ???? ??, ?????816169.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ???? Windows ???? ?? ???? ????????? ?? ??? ?? ????? ????????? ????? ?? ??? ???? ?? ????? ???? ??? (Windows ???? "NT ????." ?? ??? ??? ????? formerly ??) ??? ???? ?? ???, ???? ???? ????? ???? ?? ?? ?????? Windows ???? ??? ??? ?????? ?????????? ?? ????????? ?? ????????? ??? ????????? ????? ??? ?? ?? ??? ????? ????? ????????? ????? ???? ?? ??????? ???? ?? ??? ?????? ???? ?? ???? ???, ?? ???? ??????? ?? Visual Studio .NET ????? Explorer ????

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • 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

  1. ????? ????,????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio .NET??,Microsoft Visual Studio 2005?? ????-????? ????, ?? ???? ???Microsoft Visual Studio .NET??,Microsoft Visual Studio 2005.
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ???????? ?????????????? ????? ???, ????? ????? ?? ???? ????:
    1. ?? ???????????????? ??????????? ????,Visual Basic ?????????????? ???? ??Windows?? ???????Visual Basic.
    2. ?? ???????????????????? ????,Windows Service.
    3. ????????????? ???,LogWriterService.
    4. ??????????????? ???,C:\?? ????-????? ????, ?? ???? ???OK.
  4. ?????? Explorer ???, ???? ?????Service1.vb?? ????-????? ????, ?? ???? ???????? ???.
  5. ?????OnStartevent handler, replace the comments with the following code.
    EventLog.WriteEntry("My simple service started.")
  6. ?????? Explorer, ??? ??? ????? ????Service1.vb.
  7. ????????????? ????? ???, ????? ????Add Installer.
  8. ????????dialog box for ServiceInstaller1, change theServiceName???? ?? ??? ???LogWriterService.
  9. In Design view, clickServiceProcessInstaller1in the Code Editor.
  10. ????????????? ????? ???, ????????? ???????????? ?? ??? ???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. ??? ???? ?? ???, ????? ????? ?? ???? ????::
  1. Add a new project to your LogWriterService project.
    1. ?????? Explorer ???, ???? ?????Solution 'LogWriterService'?? ????? ????,add?? ????-????? ????, ?? ???? ?????? ?????????.
    2. ?? ???????????????? ??????????? ????,????? ?? ????????? ???????????,Setup and Deployment.
    3. ?? ???????????????????? ????,????? ?????????.
    4. ????????????? ???,ServiceSetup.
    5. ??????????????? ???,C:\?? ????-????? ????, ?? ???? ???OK.
  2. Tell the deployment project what the deployment project will package.
    1. ?????? Explorer ???, ???? ?????ServiceSetup?? ????? ????,add?? ????-????? ????, ?? ???? ???????????? ??????.
    2. ?????Add Project Output Group????? ????? ???, ????? ????LogWriterService??????????????????? ????
    3. ????? ????,???????? ???????? ????-????? ????, ?? ???? ???OK.
  3. For correct installation, you have to add only primary output. To add the custom actions, follow these steps:
    1. ?????? Explorer ???, ???? ?????ServiceSetup?? ????? ????,??????? ????-????? ????, ?? ???? ???Custom Actions.
    2. ????-????? ????Custom Actions?? ????-????? ????, ?? ???? ???Add Custom Action.
    3. ????? ????,Application Folder?? ????-????? ????, ?? ???? ???OK.
    4. ????? ????,Primary output from LogWriterService (Active)?? ????-????? ????, ?? ???? ???OK. ?? ?????Primary outputappears underInstall, Commit, Rollback and Uninstall.
  4. By default, Setup projects are not included in the build configuration. To build the solution, follow these steps:
    1. ????? ??? ?? ???? ?? ???? ?? ????? ????::
      • ????-????? ????LogWriterService?? ????-????? ????, ?? ???? ????????. Then, right-clickServiceSetup?? ????-????? ????, ?? ???? ????????.
      • To build the whole solution at the same time, clickConfiguration Manager????? ????????????? ?? ?? ??? ???? ?? ??? ???? ?????????? ??? ??? ?????ServiceSetup.
    2. Press CTRL+SHIFT+B to build the whole solution. When the solution is built, you have a complete Setup package for the service.
  5. To install the service, right-clickServiceSetup?? ????-????? ????, ?? ???? ?????????? ????.
  6. ?????ServiceSetup????? ????? ???, ????? ????next??? ???? Notice that a progress bar appears while the Setup program is installing the service.
  7. 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

Verify that the Windows Service works

  1. ????? ????,????????? ????? ????,???????? ?????? ????? ????,???????????? ??????? ????-????? ????, ?? ???? ?????????.
  2. ????-????? ????LogWriterService?? ????-????? ????, ?? ???? ??????????.
  3. 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:

      My simple service started

?????? ??????

Framework SDK ???????? ????? ????? ??:
????????? meaningful ????? ?? ????? ?? ???? ???? ???? ?? ?????? ???????? ????? ????? ???? ???? ????????? ????????? ????? ?? ?? ?? ????? ?? ??????? ???? ?????? ??? ?? ???? ???? ?? F5 ?? F11 ??? ?? ???? ???? ????????? ?? ????? ???? ?? ????; ???? ????? ????? ???? ?? ??? ??? ???? ???? ???? ????, ?? ?????? ??????? ?? ???? ???? ??????? ????, ?? ?? ???? ?? ????????? ??? ????? ??????? ?????
???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????::
(VS.71) http://MSDN.Microsoft.com/en-us/library/aa984342 .aspx

??????

????? ?? ??? ???? ???? ?? ???? ??? ??????? ?? ??? ?? ?????? Windows ???? ?? ?? ?? ????????? ???? ?? ?? ???? ??????? ???? ?? ???, ?? ??????? ???? ????, ?? ???? ???? ???? ?? ??? ???? ????? MSDN ??? ???? ?????:
(VS.71) http://msdn2.Microsoft.com/en-us/library/Aa983650 .aspx
???? ??????? ?? ???, ?????????? MSDN ??? ???? ?? ????::
HTTP://MSDN.Microsoft.com/msdnmag/issues/01/12/NETServ/default.aspx

???

???? ID: 317421 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic 6.0 Standard Edition
  • Microsoft Visual Basic 2008 Express Edition
??????: 
kbvs2005applies kbvs2005swept kbhowtomaster kbmt KB317421 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:317421

??????????? ???