如何使用visual basic.net express版安装Windows服务?

时间:2009-12-08 22:27:38

标签: .net windows vb.net service visual-studio-express

编辑:我在这个问题上开始获得赏金。目前,我已经开始使用VS2010 Pro Beta开发我的应用程序,但我真的希望能够使用快速版本构建,因为我们通常不是.net商店,即使是一个或者两个开发人员拥有VS PRO,我们整个团队都无法使用它。

要成为接受的答案并声明赏金,您必须提供示例代码和说明,以便使用vb 2008 express Edition安装和卸载Windows服务。您不一定需要从我的代码开始(但它的基本内容包含在下面)。


我编写了一个VB.NET应用程序,我希望将其作为服务运行。目前我使用的是VB.net Express Edition(2008),它没有附带“服务”模板,但我添加了一个Service类(继承自ServiceBase)和一个Installer类(继承自Installer);在这两种情况下,我都在关注MSDN的示例代码。不幸的是,我无法将此代码安装并作为服务运行。

此代码的内容是名为sampleListener的TCP侦听器类。如果我将sampleListener类设置为启动对象并运行我的项目,它可以作为控制台应用程序运行。

有一个服务类(下面),它只是启动sampleListener。

Public Class sampleSocketService
    Inherits System.ServiceProcess.ServiceBase

    Public Sub New()
        Me.ServiceName = "sample Socket Service"
        Me.CanStop = True
        Me.CanPauseAndContinue = True
        Me.AutoLog = True
    End Sub

    Shared Sub Main()
        System.ServiceProcess.ServiceBase.Run(New sampleSocketService)
    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        sampleListener.Main()
    End Sub

End Class

还有一个安装程序类,我认为是我的问题的根源。这是我最初写的安装程序类。

Imports System
Imports System.Collections
Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.ComponentModel

<RunInstallerAttribute(True)> _
Public Class sampleSocketServiceInstaller
    Inherits Installer
    Private serviceInstaller1 As ServiceInstaller
    Private processInstaller As ServiceProcessInstaller

    Public Sub New()
        ' Instantiate installers for process and services.
        processInstaller = New ServiceProcessInstaller()
        serviceInstaller1 = New ServiceInstaller()

        processInstaller.Account = ServiceAccount.LocalSystem
        serviceInstaller1.StartType = ServiceStartMode.Automatic

        ' ServiceName must equal those on ServiceBase derived classes.            
        serviceInstaller1.ServiceName = "sample Socket Service"

        ' Add installers to collection. Order is not important.
        Installers.Add(serviceInstaller1)
        Installers.Add(processInstaller)
    End Sub
End Class

在此上运行installutil.exe会产生以下消息:

An exception occurred during the Install phase.
System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.

这看起来像是一个安全问题,但我正在使用“以管理员身份运行”打开的cmd窗口中运行。

我尝试了一个基于在线示例的简化的安装程序类:

Imports System.ComponentModel
Imports System.Configuration.Install

<RunInstaller(True)> Public Class ProjectInstaller
    Inherits System.Configuration.Install.Installer

End Class

这看起来非常简单,我无法弄清楚它是如何工作的,事实上它并没有。但是,在使用此版本的安装程序类的项目上运行installutil.exe时,installutil.exe不会抛出错误消息并报告该服务已成功安装。

我怀疑我的安装程序类中的代码需要执行我的第一个示例中的一些代码,但不会执行导致错误的任何部分。

有什么建议吗?

(为了清晰起见,已对此进行了大量编辑,并添加了最初未包含的代码示例)

3 个答案:

答案 0 :(得分:6)

这似乎对我有用,但我没有在你自己的代码中添加。

创建Service1.vb和ProjectInstaller.vb两个文件。通常,Service1和ProjectInstaller被设置为部分类,但为了在此处发布,它们不是。我不认为它有任何副作用,但其他人可以评论。

我通常使用bat文件处理安装/卸载。

添加两个对项目的引用

System.ServiceProcess
System.Configuration.Install

Service1.vb

Imports System.ServiceProcess

Public Class Service1
Inherits System.ServiceProcess.ServiceBase

Protected Overrides Sub OnStart(ByVal args() As String)
End Sub

Protected Overrides Sub OnStop()
End Sub

<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

<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase

    ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}

    System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

Private components As System.ComponentModel.IContainer

<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    components = New System.ComponentModel.Container()
    Me.ServiceName = "Service1"
End Sub

End Class

ProjectInstaller.vb

Imports System.ComponentModel
Imports System.Configuration.Install

<System.ComponentModel.RunInstaller(True)> _
Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer

Public Sub New()
    MyBase.New()

    InitializeComponent()

End Sub

<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

Private components As System.ComponentModel.IContainer

<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
    Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller

    Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
    Me.ServiceProcessInstaller1.Password = Nothing
    Me.ServiceProcessInstaller1.Username = Nothing

    Me.ServiceInstaller1.ServiceName = "Service1"
    Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic

    Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})

End Sub
Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller

End Class

安装蝙蝠

C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
installutil "C:\Visual Studio 2008\Projects\....\Temp.exe"
pause
NET START Service1

Unstall Bat

C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
NET STOP Service1
installutil /u "C:\Visual Studio 2008\Projects\....\Temp.exe"

希望这对你有用

答案 1 :(得分:0)

答案 2 :(得分:0)

你可以做到,但我对你的解释感到有点困惑。您是否安装了该服务,或者您是否尝试将该服务作为控制台应用程序运行?您需要安装该服务,以便它已注册并从服务管理器运行它。或者你可以构建一个在mainstart方法中运行代码的“main”方法,但是你不能通过将服务类设置为启动方法来调试调试模式下的onstart / onstop / pause等方法。

你可以发布你的服务类(或至少足够的,以便我们可以看到你的代码)?