I'm trying to create a windows service in VB.Net using visual studio express 2012. Essentially all I want is for the service to sent a HTTP GET request to a pre-determined URL every N minutes. Kind of like a heart beat, so we know that the server is running and online.
I've got as far as creating the windows server, building the project to an .EXE and then using InstallUtil to install it as a service. This appears to be working, I following the tutorial here: http://www.dotheweb.net/2009/11/creating-services-with-vb-express/
I removed some of the code from the tutorial which writes to the windows system logs (or rather I think it creates a new log) as that doesn't work with my version of Visual Studio for some reason.
I am using the following code to send the HTTP request:
Dim url As String = "http://someURL.com/test.php"
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "GET"
Dim response As WebResponse = request.GetResponse()
The PHP file simply sends me an email when it is accessed.
The code works fine when I run the project from within visual studio (if I ignore the message informing me a windows service project should not be run like this and leave it going I do start to get emails).
However, when I fire up the windows service itself I don't get any emails however I don't get any errors appearing anywhere either.
My guess is you are using a System.Windows.Forms.Timer . That timer will not work without a System.Windows.Forms.Form . The timer to use in a windows service is System.Timers.Timer
Declare it in your Service Class
Private WithEvents m_timer As System.Timers.Timer
Start it in the OnStart method:
m_timer = New System.Timers.Timer(300000) ' 5 minutes
m_timer.Enabled = True
And handle the Elapsed event of the timer
Private Sub m_timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_timer.Elapsed
m_timer.Enabled = False
Dim url As String = "http://someURL.com/test.php"
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "GET"
Dim response As WebResponse = request.GetResponse()
m_timer.Enabled = True
End Sub
Don't forget to stop the timer in the OnStop method.