I have created my first windows service and have found that it fails
when I initially start it but when I immediately try to restart the
service it starts. What could be causing this behavior? I have
included my OnStart code below. - Thanks
Protected Overrides Sub OnStart(ByVal args() As String)
'Get settings from configuration file
AppSettings = New System.Configuration.AppSettingsReader
Dim StartUpEntry As New System.Text.StringBuilder
Dim TestModeActive As Boolean = True
Monitor_AuthLocation_Timer.Enabled = False
AddHandler Monitor_AuthLocation_Timer.Elapsed, AddressOf
Monitor_AuthLocation_Timer_Tick
Try
StartUpEntry.Append("Started " & System.DateTime.Now &
vbCrLf)
'determine if in testmode
If Not (CType(AppSettings.GetValue("Mode",
GetType(System.String)), System.String).Trim.ToUpper.Equals("TEST"))
Then
TestModeActive = False
End If
'set timer interval
Try
Monitor_AuthLocation_Timer.Interval =
CType(AppSettings.GetValue("Monitor_AuthLocation_T imer.Interval",
GetType(System.String)), System.Double)
Catch ex As Exception
Monitor_AuthLocation_Timer.Interval = 0
End Try
If Monitor_AuthLocation_Timer.Interval <= 0 Or
Monitor_AuthLocation_Timer.Interval 10000 Then
Monitor_AuthLocation_Timer.Interval = 2000 ' default
to 2 seconds
End If
'establish filepaths based upon which mode of option is
being used
If TestModeActive = False Then
AuthLocation =
CType(AppSettings.GetValue("FilePath_AuthLocation" ,
GetType(System.String)), System.String)
RespLocation =
CType(AppSettings.GetValue("FilePath_RespLocation" ,
GetType(System.String)), System.String)
Else
AuthLocation =
CType(AppSettings.GetValue("FilePath_AuthLocation_ Test",
GetType(System.String)), System.String)
RespLocation =
CType(AppSettings.GetValue("FilePath_RespLocation_ Test",
GetType(System.String)), System.String)
End If
StartUpEntry.Append("AuthLocation = " & AuthLocation &
vbCrLf)
StartUpEntry.Append("RespLocation = " & RespLocation &
vbCrLf)
'Create authlocation if it does not already exist
If System.IO.Directory.Exists(AuthLocation) = False Then
System.IO.Directory.CreateDirectory(AuthLocation)
StartUpEntry.Append("AuthLocation not found...created.
" & vbCrLf)
End If
'Create resplocation if it does not already exist
If System.IO.Directory.Exists(RespLocation) = False Then
System.IO.Directory.CreateDirectory(RespLocation)
StartUpEntry.Append("RespLocation not found...created.
" & vbCrLf)
End If
'Write startup entry
AppErrLog.WriteEntry(StartUpEntry.ToString)
'establish credentials
Dim credentials As New
System.Net.NetworkCredential(username, password)
WebService.Credentials = credentials
WebService.PreAuthenticate = True
'enable monitoriing of auth location
Monitor_AuthLocation_Timer.Enabled = True
Catch ex As Exception
ex.Source = "OnStart"
Call HandleError(ex)
End Try
End Sub
Private Sub Monitor_AuthLocation_Timer_Tick(ByVal sender As
System.Object, ByVal e As System.Timers.ElapsedEventArgs)
Call Monitor_AuthLocation()
End Sub