mk_b
asked on
windows service not running
please help im having some problems running this code as a windows service? It code workd fine as a applicationbut not as a service?
i have tryed degugging it an attached bugging to the process an it nerver hits the break point which i put on the "If File.Exists(ipPath) Then" line ?
But i now the service is started because it hits MyLog.WriteEntry("blackIp Log", "MyCheck: blackip started -" & CStr(TimeOfDay), EventLogEntryType.Informat ion) line and puts in the Event log.....
any help will be apreciated...
Imports System.ServiceProcess
Imports System.IO
Imports System.Data
Public Class blackIp
Inherits System.ServiceProcess.Serv iceBase
Dim MyLog As New EventLog
Private ipPath As String ' = "c:\inetpub\wwwroot\w3.ip. com\curren t\theip.tx t"
Private fPath As String ' = "C:\Program Files\programe\test.ini"
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.Serv iceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.Serv iceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.Serv iceBase() {New blackIp}
System.ServiceProcess.Serv iceBase.Ru n(Services ToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.ICon tainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Cont ainer
Me.Timer1 = New System.Windows.Forms.Timer (Me.compon ents)
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 10000
'
'blackIp
'
Me.ServiceName = "blackIp"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
If Not MyLog.SourceExists("blackI p") Then
MyLog.CreateEventSource("b lackIp", "blackIp Log")
End If
MyLog.Source = "blackIp"
Try
Dim ds = New DataSet
ds.ReadXml(System.Windows. Forms.Appl ication.St artupPath & "\settings.xml")
ipPath = ds.Tables(0).Rows(0).Item( "value")
fPath = ds.Tables(0).Rows(1).Item( "value")
Catch ex As Exception
MyLog.WriteEntry("blackIp Log", "Error: " & ex.Message & "-" & CStr(TimeOfDay), EventLogEntryType.Informat ion)
End Try
MyLog.WriteEntry("blackIp Log", "MyCheck: blackip started -" & CStr(TimeOfDay), EventLogEntryType.Informat ion)
Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If File.Exists(ipPath) Then
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(ipPath)
strContents = objReader.ReadLine
objReader.Close()
'--add to black ice settings
SaveTextToFile(strContents )
'--Delete file
File.Delete(ipPath)
MyLog.WriteEntry("blackIp Log", "IpChanged: " & strContents & " - " & CStr(TimeOfDay), EventLogEntryType.Informat ion)
Catch Ex As Exception
MyLog.WriteEntry("blackIp Log", "Error: " & Ex.Message & "-" & CStr(TimeOfDay), EventLogEntryType.Informat ion)
End Try
End If
End Sub
Public Function SaveTextToFile(ByVal newIp As String) As String
Dim Contents, strData As String
Dim bAns As String = "False"
Dim objWriter As StreamWriter
Dim objReader As StreamReader
Try
'--read the file and change the currentIp line
objReader = New StreamReader(fPath)
Dim lines As Array = Split(objReader.ReadToEnd( ), vbCrLf)
Dim i As Integer
For i = 0 To lines.GetUpperBound(0)
If InStr(1, lines(i), "[MANUAL IP ACCEPT]") Then
'ACCEPT, 111.111.111.111,CurrentIp, 2004-11-30 12:22:47, 2004-12-01 12:22:47, 2000, BIgui
strData &= lines(i) & vbCrLf
strData &= "ACCEPT, " & newIp & ", CurrentIp, " & GetMySQLDate() & ", " & GetMySQLDatePlus() & ", 2000, BIgui" & vbCrLf
ElseIf InStr(1, lines(i), "CurrentIp") Then
Else
strData &= lines(i) & vbCrLf
End If
Next
objReader.Close()
'--write to the file
objWriter = New StreamWriter(fPath)
objWriter.Write(strData)
objWriter.Close()
bAns = True
Catch Ex As Exception
bAns = Ex.Message
End Try
Return bAns
End Function
Public Function GetMySQLDate() As String
Dim dtNow As DateTime = DateTime.Now
GetMySQLDate = dtNow.Year & "-" & PadZero(dtNow.Month) & "-" & PadZero(dtNow.Day) & " " & dtNow.Hour & ":" & dtNow.Minute & ":" & dtNow.Second
End Function
Public Function GetMySQLDatePlus() As String
Dim dtNow As DateTime = DateTime.Now
GetMySQLDatePlus = dtNow.Year & "-" & PadZero(dtNow.Month) & "-" & PadZero(dtNow.Day + 1) & " " & dtNow.Hour & ":" & dtNow.Minute & ":" & dtNow.Second
End Function
Public Function PadZero(ByVal val) As String
If Len(CStr(val)) = 1 Then
PadZero = "0" & CStr(val)
Else
PadZero = val
End If
End Function
End Class
i have tryed degugging it an attached bugging to the process an it nerver hits the break point which i put on the "If File.Exists(ipPath) Then" line ?
But i now the service is started because it hits MyLog.WriteEntry("blackIp Log", "MyCheck: blackip started -" & CStr(TimeOfDay), EventLogEntryType.Informat
any help will be apreciated...
Imports System.ServiceProcess
Imports System.IO
Imports System.Data
Public Class blackIp
Inherits System.ServiceProcess.Serv
Dim MyLog As New EventLog
Private ipPath As String ' = "c:\inetpub\wwwroot\w3.ip.
Private fPath As String ' = "C:\Program Files\programe\test.ini"
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.Serv
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.Serv
'
ServicesToRun = New System.ServiceProcess.Serv
System.ServiceProcess.Serv
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.ICon
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.Debugg
Me.components = New System.ComponentModel.Cont
Me.Timer1 = New System.Windows.Forms.Timer
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 10000
'
'blackIp
'
Me.ServiceName = "blackIp"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
If Not MyLog.SourceExists("blackI
MyLog.CreateEventSource("b
End If
MyLog.Source = "blackIp"
Try
Dim ds = New DataSet
ds.ReadXml(System.Windows.
ipPath = ds.Tables(0).Rows(0).Item(
fPath = ds.Tables(0).Rows(1).Item(
Catch ex As Exception
MyLog.WriteEntry("blackIp Log", "Error: " & ex.Message & "-" & CStr(TimeOfDay), EventLogEntryType.Informat
End Try
MyLog.WriteEntry("blackIp Log", "MyCheck: blackip started -" & CStr(TimeOfDay), EventLogEntryType.Informat
Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If File.Exists(ipPath) Then
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(ipPath)
strContents = objReader.ReadLine
objReader.Close()
'--add to black ice settings
SaveTextToFile(strContents
'--Delete file
File.Delete(ipPath)
MyLog.WriteEntry("blackIp Log", "IpChanged: " & strContents & " - " & CStr(TimeOfDay), EventLogEntryType.Informat
Catch Ex As Exception
MyLog.WriteEntry("blackIp Log", "Error: " & Ex.Message & "-" & CStr(TimeOfDay), EventLogEntryType.Informat
End Try
End If
End Sub
Public Function SaveTextToFile(ByVal newIp As String) As String
Dim Contents, strData As String
Dim bAns As String = "False"
Dim objWriter As StreamWriter
Dim objReader As StreamReader
Try
'--read the file and change the currentIp line
objReader = New StreamReader(fPath)
Dim lines As Array = Split(objReader.ReadToEnd(
Dim i As Integer
For i = 0 To lines.GetUpperBound(0)
If InStr(1, lines(i), "[MANUAL IP ACCEPT]") Then
'ACCEPT, 111.111.111.111,CurrentIp,
strData &= lines(i) & vbCrLf
strData &= "ACCEPT, " & newIp & ", CurrentIp, " & GetMySQLDate() & ", " & GetMySQLDatePlus() & ", 2000, BIgui" & vbCrLf
ElseIf InStr(1, lines(i), "CurrentIp") Then
Else
strData &= lines(i) & vbCrLf
End If
Next
objReader.Close()
'--write to the file
objWriter = New StreamWriter(fPath)
objWriter.Write(strData)
objWriter.Close()
bAns = True
Catch Ex As Exception
bAns = Ex.Message
End Try
Return bAns
End Function
Public Function GetMySQLDate() As String
Dim dtNow As DateTime = DateTime.Now
GetMySQLDate = dtNow.Year & "-" & PadZero(dtNow.Month) & "-" & PadZero(dtNow.Day) & " " & dtNow.Hour & ":" & dtNow.Minute & ":" & dtNow.Second
End Function
Public Function GetMySQLDatePlus() As String
Dim dtNow As DateTime = DateTime.Now
GetMySQLDatePlus = dtNow.Year & "-" & PadZero(dtNow.Month) & "-" & PadZero(dtNow.Day + 1) & " " & dtNow.Hour & ":" & dtNow.Minute & ":" & dtNow.Second
End Function
Public Function PadZero(ByVal val) As String
If Len(CStr(val)) = 1 Then
PadZero = "0" & CStr(val)
Else
PadZero = val
End If
End Function
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this may not be a timer selection error... see the EVENTLOG and find out the error
ASKER
Thanks Andy, spot on....
../mk
../mk
Your welcome. Been there, done that when I first started out on windows services.