dav*_*dcl 1 .net vb.net windows 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
Run Code Online (Sandbox Code Playgroud)
还有一个安装程序类,我认为是我的问题的根源.这是我最初写的安装程序类.
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 Code Online (Sandbox Code Playgroud)
在此上运行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.
Run Code Online (Sandbox Code Playgroud)
这看起来像是一个安全问题,但我正在使用"以管理员身份运行"打开的cmd窗口中运行.
我尝试了一个基于在线示例的简化的安装程序类:
Imports System.ComponentModel
Imports System.Configuration.Install
<RunInstaller(True)> Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer
End Class
Run Code Online (Sandbox Code Playgroud)
这看起来非常简单,我无法弄清楚它是如何工作的,事实上它并没有.但是,在使用此版本的安装程序类的项目上运行installutil.exe时,installutil.exe不会抛出错误消息并报告该服务已成功安装.
我怀疑我的安装程序类中的代码需要执行我的第一个示例中的一些代码,但不会执行导致错误的任何部分.
有什么建议?
(为了清晰起见,已对此进行了大量编辑,并添加了最初未包含的代码示例)
这似乎对我有用,但我没有在你自己的代码中添加.
创建Service1.vb和ProjectInstaller.vb两个文件.通常,Service1和ProjectInstaller被设置为部分类,但为了在此处发布,它们不是.我不认为它有任何副作用,但其他人可以评论.
我通常使用bat文件处理Install/Uninstall.
添加两个对项目的引用
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
安装蝙蝠
C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
installutil "C:\Visual Studio 2008\Projects\....\Temp.exe"
pause
NET START Service1
Run Code Online (Sandbox Code Playgroud)
卸下蝙蝠
C:
CD \WINDOWS\Microsoft.NET\Framework\v2.0.50727
NET STOP Service1
installutil /u "C:\Visual Studio 2008\Projects\....\Temp.exe"
Run Code Online (Sandbox Code Playgroud)
希望这对你有用