【问题标题】:Service work in debug mode only服务仅在调试模式下工作
【发布时间】:2018-03-08 00:50:38
【问题描述】:

不明白为什么我的 Windows 服务只能在调试模式下工作。

只使用了三个函数。

<System.Diagnostics.DebuggerNonUserCode()>
Shared Sub Main()

    'Dim servicio As New Service1
    'servicio.OnStart(Nothing)
    'System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)

    WriteLog("Starting it")
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

Public Sub MainWorker()
    While 1 = 1
        Try
            Dim mLoop As New Init
        Catch ex As Exception

        End Try
    End While
End Sub

虽然它处于调试模式,但它作为方法 OnStart 的魅力。

  Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.

        Try
            Dim T As New Thread(AddressOf MainWorker)
            T.Start()
        Catch ex As Exception

        End Try

    End Sub

但是当它在没有调试的情况下使用(安装并运行)时,它不会弹出任何错误消息。状态正在运行。但什么也没有发生。甚至没有日志消息“正在启动”...

编辑:pastebin 上的初始化类PASTEBIN.INIT

【问题讨论】:

  • 它可能不会弹出错误消息,因为您正在吞噬您的异常。尝试在你的 catch 块中加入某种日志记录。
  • @tgolisch 我把记录器放回去了。似乎服务根本没有运行这个初始化,甚至没有继续 OnStart 方法:S
  • 检查您的 Windows 事件日志。他们是否在您的服务启动时捕获任何错误?
  • @tgolisch 没有捕获任何错误

标签: vb.net service


【解决方案1】:

主要问题是当我像这样放置日志文件的路径时,出于某种原因,我应该跳出框框思考

Public Sub WriteLog2(sLine As String)
        Dim strFile As String = "Service_Log.txt"
        Dim fileExists As Boolean = File.Exists(strFile)
        Using sw As New StreamWriter(File.Open(strFile, FileMode.Append))
            sw.WriteLine(sLine)
        End Using

        Console.WriteLine(sLine)
    End Sub

它不起作用,因为我需要输入 Service_Log.txt 文件的完整路径。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多