我创建了一个简单的Windows服务。这是我的第一次尝试,所以也许我做错了什么。无论如何,服务应该自动启动,但事实并非如此。它会启动但会自动停止,而不会报告任何错误。如果我通过计算机管理器窗口手动启动它,它可以正常工作,永远不会停止...
我也尝试记录异常,但没有结果。
这是onStart()代码:
Private _conn As SqlConnection
Private _connAttemps As Integer = 0
Protected Overrides Sub OnStart(ByVal args() As String)
Try
tryConn() 'Tries to connect and report the connection object on success
If _conn Is Nothing Then
If _connAttemps = 3 Then
Return
Else
setTimeout(Nothing, New Action(AddressOf tryConn), 10000, Nothing)
End If
End If
' Create a timer.
Dim aTimer As Timer = New Timer()
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Set the Interval to 30 seconds (30000 milliseconds).
aTimer.Interval = 30000
aTimer.Enabled = True
GC.KeepAlive(aTimer)
Catch ex As Exception
Dim appLog As EventLog = New EventLog()
If Not EventLog.SourceExists("WindowServiceProva") Then
EventLog.CreateEventSource("WindowServiceProva", "WindowServiceProvaLog")
End If
appLog.Source = "WindowServiceProva"
appLog.WriteEntry("Eccezione: " & ex.ToString)
End Try
End Sub
这就是安装程序:
Public Class myInstaller
Private serviceInstaller As ServiceInstaller
Private processInstaller As ServiceProcessInstaller
Public Sub New()
MyBase.New()
'Chiamata richiesta da Progettazione componenti.
InitializeComponent()
'Aggiungere il codice di inizializzazione dopo la chiamata a InitializeComponent
processInstaller = New ServiceProcessInstaller()
serviceInstaller = New ServiceInstaller()
'Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem
'Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Automatic
serviceInstaller.ServiceName = "WindowServiceProva"
'Per evitare che il servizio di interrompa
serviceInstaller.DelayedAutoStart = True
Installers.Add(serviceInstaller)
Installers.Add(processInstaller)
End Sub
End Class
提前感谢您的帮助!