Windows Web服务自动启动和停止

时间:2014-08-15 18:44:01

标签: vb.net windows-services windows-7-x64

运行Windows服务时会显示此消息。

The [service name] service on local computer started and then stopped. 
Some Services stop automatically if they are not in use by another services or programs.

我不确定导致此错误的原因。以下是我服务的代码。我的代码使用另一个名为MagentoSalesOrder的类。我首先将此代码作为控制台应用程序运行,它运行得很好。我相信这是导致错误的原因。当我注释掉使用该类的行时,我的服务可以很好地将测试打印到文件中。

导入MyFirstService.MagentoSalesOrder 公共类MyFirstService     Dim WithEvents timer1 As New System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)
    timer1.Interval = 10000
    timer1.Start()
    WriteLog(Me.ServiceName & " has started ...")

End Sub

Protected Overrides Sub OnStop()
    WriteLog(Me.ServiceName & " has stopped ...")
End Sub

Private Sub timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
    WriteLog(Me.ServiceName & " is running ...")
End Sub

Private Sub WriteLog(ByVal strMessage As String)
    Dim strPath As String, file As System.IO.StreamWriter
    Dim test As New MagentoSalesOrder()
    strPath = AppDomain.CurrentDomain.BaseDirectory & "\MyService.log"
    file = New System.IO.StreamWriter(strPath, True)
    Dim arr = test.BuildPreOrder()
    If (arr.Length > 0) Then

        For Each element As Long In arr
            file.WriteLine("PreOrder created: " + element)
        Next
    Else
        file.WriteLine("No orders to process")
    End If
    'file.WriteLine("Test")
    file.Close()
End Sub
End Class

所以我发现我的错误来自我的foreach循环中的file.writeline。

在我的writefile中将元素更改为 element.toString 修复了我的服务。

1 个答案:

答案 0 :(得分:0)

问题来自于尝试将Long连接到String。

在我的writefile中将元素更改为element.toString修复了我的服务。