繁体   English   中英

如何用Visual Basic.net Express Edition安装Windows服务?

[英]How can a Windows service be installed with visual basic.net express edition?

编辑:我开始悬赏这个问题。 目前,我已经开始使用VS2010 Pro Beta开发我的应用程序,但是我真的希望它可以使用Express Edition构建,因为我们通常不是.net商店,即使我们不是.net商店,两名开发人员拥有VS PRO,因此我们整个团队都无法使用。

要获得被接受的答案并获得赏金,您必须提供示例代码和说明,以允许使用vb 2008 Express Edition安装和卸载Windows Service。 您不一定需要从我的代码开始(但是其基本内容包含在下面)。


我已经编写了一个VB.NET应用程序,我想将其作为服务运行。 当前,我正在使用VB.net Express Edition(2008),该版本不附带“ Service”模板,但是我添加了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窗口中运行。

我根据在线示例尝试了一个简化得多的Installer类:

Imports System.ComponentModel
Imports System.Configuration.Install

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

End Class

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

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

有什么建议么?

(为了清楚起见,对它进行了广泛的编辑,并添加了最初未包含的代码示例)

这似乎对我有用,但是我没有添加您自己的代码。

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

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

向项目添加两个引用

System.ServiceProcess
系统配置安装

服务1.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

失速蝙蝠

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

希望对你有用

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

您可以发布您的服务类(或至少发布它,以便我们可以看到您的代码)吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM