首页 > 解决方案 > Windows 服务的奇怪问题 - 服务超时

问题描述

您好,我有一段遗留的(VS2010)Windows 服务代码已导入 VS2017,这让我非常沮丧。这段代码在过去 6 年里运行良好,但是当我执行安装并尝试启动服务时,SCM 会返回超时错误。OnStart 代码如下:

Protected Overrides Sub OnStart(ByVal args() As String)
    'Instaniate the timer for the service
    _serviceTimer = New Threading.Timer(New Threading.TimerCallback(AddressOf Tick), Nothing, 60000, 60000)
End Sub

回调是:

Private Sub Tick(ByVal state As Object)

    'Switch off the timer event whilst the code executes
    _serviceTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite)

    If Not _started Then
        Startup()
        _started = True
    End If

    Call ServerProcess()

    'Re-enable the timer now that application code has completed
    _serviceTimer.Change(_longInterval, _longInterval)
End Sub

我最初在 OnStart 方法中有启动过程,但为了解决此问题而将其删除,但它没有任何区别。方法启动如下:

Public Sub Startup()

    Try
        'Source these settings from the local config file
        _appDataFolder = Utilities.GetSetting("AppDataRoot")
        _configPathMapped = _appDataFolder & Utilities.GetSetting("ConfigPathMapped")
        _logPath = _appDataFolder & "\" & utl.GetSetting("LogPath")

        'Instaniate the timer for the service - Commented out after moving startup code from OnStart method
        ' _serviceTimer = New Threading.Timer(New Threading.TimerCallback(AddressOf Tick), Nothing, Timeout.Infinite, Timeout.Infinite)

        'Initialise logging architecture
        _logger = New aslLog.Logger(_configPathMapped & "nlog.config", _logPath, My.Application.Info.ProductName, My.Application.Info.Version.ToString)
        _logger.SendLog("Started PSALERTS Schedule Server Service", NLog.LogLevel.Info, _serviceTimer, _checkInterval, Nothing)

        'Determine if the cloned config files exists in the mapped config file folder
        'We clone these files to a writable destination to allow us to overcome write restrictions ot the C: drive on the SPEN PTI Desktop model
        If Not System.IO.File.Exists(_configPathMapped & "psaservermachine.config") Then
            'Clone the app.config file in the config folder as psaservermachine.config
            Utilities.CloneFile(_programFileLocation & "PSALERTSScheduleServer.exe.config", _configPathMapped & "psaservermachine.config")
        End If

        If Not System.IO.File.Exists(_configPathMapped & "nlog.config") Then
            'Clone the nlog.config file
            Utilities.CloneFile(_programFileLocation & "PSALERTSScheduleServer.exe.config", _configPathMapped & "nlog.config")
        End If

        'Determine the Oracle TNS Environment
        'Check for the existence of the environment variable 'TNS_ADMIN'
        If Environment.GetEnvironmentVariable("TNS_ADMIN") IsNot Nothing Then

            'If TNS_ADMIN exists then we can continue with the application session 

        Else
            Dim oraTnsPath As String = ""
            'If it doesn't exist then we need to determine the Oracle information from the PATH environment variable
            oraTnsPath = GetOraTnsPath()

            If oraTnsPath <> "" Then
                'Then create the TNS_ADMIN environment variable
                Environment.SetEnvironmentVariable("TNS_ADMIN", oraTnsPath)
            Else

                'If no oracle client information exists then raise an error to this effect and exit the app
                'informing the user that they need to install the Oracle client in order to use PSALERTS

                Beep()
                Throw New PSALERTSOracleConfigException(
                                                "PSALERTS Oracle Configuration Error. PSALERTS Did not find a valid Oracle Client." & vbCrLf & vbCrLf &
                                                "Please install a valid Oracle Client and try again." & vbCrLf & vbCrLf &
                                                "If a valid Oracle Client is installed then ensure that the PATH environment variable contains an entry for the Oracle Client." & vbCrLf & vbCrLf &
                                                "For example - TNS_ADMIN=C:\oracle\12.1.0\Client_lite\NETWORK\ADMIN"
                                                )

            End If

        End If

        'Register the application
        If Not Registered() Then
            'Register the application
            Register()

        End If


        If Registered() Then
            'Clean/close any stray Excel processes from previous debug session
            If _debugModeOn Then
                CleanUpRedundantProcesses("EXCEL", "PSALERTS")
            End If

            'instantiate fresh excel session
            _myXLApp = New Excel.Application

            'Get the timer interval settings
            _longInterval = CType(utl.GetSettingServerMachine(_configPath, "appSettings", "LongIntervalMillis"), Integer)
            _initInterval = CType(utl.GetSettingServerMachine(_configPath, "appSettings", "InitialIntervalMillis"), Integer)
            _refreshInterval = CType(utl.GetSettingServerMachine(_configPath, "appSettings", "InitialIntervalMillis"), Integer)

            'Re-start the timer with periodic signalling as per the specified check interval
            _serviceTimer.Change(_initInterval, _initInterval)

        Else
                _started = False
        End If
    Catch ex As Exception
        _logger.SendLog("PSALERTS Schedule Server startup failure.", NLog.LogLevel.Error, ex)
    Finally

    End Try
End Sub

我对许多类似的服务使用了类似的技术,它们运行良好。希望从那里的任何 Windows 服务专家那里获得一些见解。哦,我使用 WiX 进行安装,对于许多类似的此类应用程序来说,这又是一个陈旧的模板。

亲切的问候保罗J。

标签: vb.netvisual-studio-2017wixwindows-services

解决方案


核心:最典型的错误:

  • 配置问题:连接字符串、错误路径等...
  • 开机启动问题(好列表——来自FAQ)
  • 以具有密码的真实用户身份运行时密码/登录帐户错误。
  • 文件丢失运行时丢失
  • 权限问题(缺少 ACL/NT 权限)。

也许在下面之前检查这个答案


更新也许看看这个先前的答案。服务启动时间问题。还要在同一页面中查看我的临时答案。

调试器:除此之外 - 没有什么比使用调试器单步调试代码更好的了。我已经很久没有这样做了。部署调试二进制文件并尝试?Windows 10 现在隐藏来自服务的消息 - 不确定这如何影响调试器:不再切换到 Session 0


我不是服务专家,而是部署专家。我只会提供一些链接,看看是否有帮助。Maybe I have not fully understood the whole problem. I tend to focus on the deployment side and not so much development side.

想法列表/调试检查列表:这些是一般应用程序可能出错的“想法列表” - 不仅仅是服务(两个第一个列表相似 - 相隔一段时间创建):

  1. 启动时崩溃
  2. 桌面应用程序无法启动
  3. 通用 WiX / MSI 链接

是的,这些列表非常通用——太大而无法消化。只看我认为的前两个。

调试工具:还提醒最有用的服务调试工具:Event ViewerTask ManagerServices.mscProcess Explorer(系统内部)The NET commandSC.exe.

良好的服务常见问题解答 https ://www.coretechnologies.com/WindowsServices/FAQ.html


推荐阅读