Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

vb.net - Weird Issue With Windows Service - Service Timing Out

Hi there I have piece of legacy (VS2010) Windows Service code that I have imported into VS2017 and is causing me severe frustration. This code has worked well for about the last 6 years, however when I carry out the install and attempt to start the service the SCM comes back with a timeout error. The OnStart code is as follows:

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

The call back is:

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

I originally had the Startup process in the OnStart method, however removed it as an attempt at resolving this issue, however it has not made any difference. Method Startup is as follows:

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:oracle12.1.0Client_liteNETWORKADMIN"
                                                )

            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

I use a similar technique for a number of similar services and they are running fine. Would appreciate some insight from any Windows Service gurus out there. Oh, I use WiX to carry out the install, again this is a well worn template for a number of similar such applications.

Kind Regards Paul J.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Core: The very most typical errors:

  • Config problems: connection strings, faulty paths, etc...
  • Boot startup problem (good list - from FAQ)
  • Wrong password / login account when running as a real user with password.
  • Files missing or runtimes missing.
  • Permission problems (ACL / NT Privilege missing).

Maybe check this answer before the below.


UPDATE: Maybe have a look at this previous answer. Service startup timing issue. Also check my ad-hoc answer there in the same page.

Debugger: Other than that - nothing like stepping through the code with a debugger. I haven't done that in a long time. Deploy debug binaries and try? Windows 10 now hides messages from services - not sure how that affects debuggers: No more switching to Session 0.


I am not a service guru, but a deployment specialist. I'll just provide some links and see if that helps. Maybe I have not fully understood the whole problem. I tend to focus on the deployment side and not so much development side.

Ideas List / Debugging Check List: These are "ideas lists" for what could be wrong for applications in general - not just services (two first lists are similar - created some time apart):

  1. Crash on launch
  2. Desktop application won't launch
  3. General purpose WiX / MSI links

Yes, these lists are very generic - too large to digest. Just skim the first two I think.

Debugging Tools: Also a reminder of the most useful service debugging tools: Event Viewer, Task Manager, Services.msc, Process Explorer (system internals), The NET command and SC.exe.

Good Service FAQ: https://www.coretechnologies.com/WindowsServices/FAQ.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...