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

如何使用visual basic.net express edition安装Windows服务?,.net,windows,vb.net,service,visual-studio-express,.net,Windows,Vb.net,Service,Visual Studio Express,编辑:我开始悬赏这个问题。目前,我已经开始使用VS2010 Pro Beta版开发我的应用程序,但我真的希望它能够使用express edition构建,因为我们通常不是.net商店,即使一两个开发人员使用VS Pro,我们整个团队也无法使用它 要成为公认的答案并获得奖励,您必须提供示例代码和说明,以允许使用vb 2008 express edition安装和卸载Windows服务。您不一定需要从我的代码开始(但其要点包含在下面) 我已经编写了一个VB.NET应用程序,我想将其作为服务运行。目

编辑:我开始悬赏这个问题。目前,我已经开始使用VS2010 Pro Beta版开发我的应用程序,但我真的希望它能够使用express edition构建,因为我们通常不是.net商店,即使一两个开发人员使用VS Pro,我们整个团队也无法使用它

要成为公认的答案并获得奖励,您必须提供示例代码和说明,以允许使用vb 2008 express edition安装和卸载Windows服务。您不一定需要从我的代码开始(但其要点包含在下面)


我已经编写了一个VB.NET应用程序,我想将其作为服务运行。目前我使用的是VB.net Express Edition(2008),它没有附带“服务”模板,但我添加了一个服务类(从ServiceBase继承)和一个安装程序类(从安装程序继承);在这两种情况下,我都遵循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
还有一个安装程序类,我认为这是我的问题的根源。这是我最初编写的installer类

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
这看起来像是一个安全问题,但我正在使用Run As Administrator打开的cmd窗口中运行

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

Imports System.ComponentModel
Imports System.Configuration.Install

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

End Class
导入System.ComponentModel
导入System.Configuration.Install
公共类项目安装程序
继承System.Configuration.Install.Installer
末级
这看起来简单得可笑,我不知道它怎么可能工作,事实上它没有。但是,在使用此版本的安装程序类在项目上运行installutil.exe时,installutil.exe不会抛出错误消息并报告服务已成功安装

我怀疑我的installer类中需要执行第一个示例中的某些操作,但不执行导致错误的任何部分的代码

有什么建议吗


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

看看这篇文章:


看看这个线程:


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


您可以发布您的服务类(或者至少发布足够的服务类,以便我们查看您的代码)吗

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


您可以发布您的服务类(或者至少发布足够的服务类,以便我们查看您的代码)吗

这似乎对我有效,但我还没有添加您自己的代码

创建两个文件: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
不撒球球拍

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"

希望这对您有用

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

创建两个文件: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
不撒球球拍

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"

希望这对您有用

这似乎没有帮助--该线程是关于从ASP.net访问事件日志的,因此应用程序池用户需要有权限。在我的系统上,管理员已经完全控制了这些注册表项,并且安装尝试正在以管理员身份运行。这似乎没有帮助——该线程正在考虑从ASP.net访问事件日志,因此应用程序池用户需要有权限。在我的系统上,管理员已经完全控制了这些注册表项,并且安装尝试正在以管理员身份运行。当然,我已经编辑了问题以包含服务类。我的意思是,如果我将startup对象设置为sampleListener,我就可以将其作为控制台应用程序运行。安装服务以便在service manager中注册是我遇到的问题。如何安装服务:最简单的方法是只构建一个安装程序/安装程序项目,但我认为VB Express不支持taht。正确,VB Express不支持安装程序项目。我已经在尝试使用那篇文章中描述的installutil进行安装。正如我在上面的编辑中提到的,我非常确定p