Need help with my service please!!!
-
Hey guys the problem that I am is this. I am writting a service that every five minutes goes out and requests a web page. And the problem that I am having is whenever I disconnect my computer for the internet, the service throws an error. I have tried implementing some error handling but I can't seem to figure it out. I would like to code somethingso that if in 30 seconds a response is not gathered then to abort the request and try again in five minutes. Here is the code that i currently have: Imports System Imports System.IO Imports System.Net Imports System.Threading Public Class VirasecHB 'Declare the Thread Private VirasecHB_Thread As New Thread( _ New System.Threading.ThreadStart(AddressOf waitProcedure)) 'Declare Variables Dim serverID As String = "1336" Dim webURL As String = "http://www.virasec.com/intranet/tech/heart/heartdb.asp?hdur=300&hsid=" & serverID Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. WebRequestProcedure() VirasecHB_Thread.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. VirasecHB_Thread.Abort() End Sub Private Sub WebRequestProcedure() ' Create a request for the URL. Dim request As WebRequest = WebRequest.Create(webURL) ' If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials 'Sets web request timeout time request.Timeout = 30000 ' 30000 = 30 Seconds ' Get the response. Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 'Close the response request response.Close() End Sub Private Sub waitProcedure() Do 'Wait Five Mintues System.Threading.Thread.Sleep(300000) '300000 = 5 Minutes Try WebRequestProcedure() Catch ex As Exception Dim errorLog As New EventLog errorLog.WriteEntry("Virasec Heartbeat Service " & _ "has encountered an error while trying to " & _ "connect to the Virasec web server. " & _ "The service will attempt to " & _ "connect again in five minut
-
Hey guys the problem that I am is this. I am writting a service that every five minutes goes out and requests a web page. And the problem that I am having is whenever I disconnect my computer for the internet, the service throws an error. I have tried implementing some error handling but I can't seem to figure it out. I would like to code somethingso that if in 30 seconds a response is not gathered then to abort the request and try again in five minutes. Here is the code that i currently have: Imports System Imports System.IO Imports System.Net Imports System.Threading Public Class VirasecHB 'Declare the Thread Private VirasecHB_Thread As New Thread( _ New System.Threading.ThreadStart(AddressOf waitProcedure)) 'Declare Variables Dim serverID As String = "1336" Dim webURL As String = "http://www.virasec.com/intranet/tech/heart/heartdb.asp?hdur=300&hsid=" & serverID Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. WebRequestProcedure() VirasecHB_Thread.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. VirasecHB_Thread.Abort() End Sub Private Sub WebRequestProcedure() ' Create a request for the URL. Dim request As WebRequest = WebRequest.Create(webURL) ' If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials 'Sets web request timeout time request.Timeout = 30000 ' 30000 = 30 Seconds ' Get the response. Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 'Close the response request response.Close() End Sub Private Sub waitProcedure() Do 'Wait Five Mintues System.Threading.Thread.Sleep(300000) '300000 = 5 Minutes Try WebRequestProcedure() Catch ex As Exception Dim errorLog As New EventLog errorLog.WriteEntry("Virasec Heartbeat Service " & _ "has encountered an error while trying to " & _ "connect to the Virasec web server. " & _ "The service will attempt to " & _ "connect again in five minut
tcombs07 wrote:
the problem that I am having is whenever I disconnect my computer for the internet, the service throws an error.
This line might be the problem...No internet connection, no getting to www.virasec.com.......
tcombs07 wrote:
Dim webURL As String = "http://www.virasec.com/intranet/tech/heart/heartdb.asp?hdur=300&hsid=" & serverID
-
Hey guys the problem that I am is this. I am writting a service that every five minutes goes out and requests a web page. And the problem that I am having is whenever I disconnect my computer for the internet, the service throws an error. I have tried implementing some error handling but I can't seem to figure it out. I would like to code somethingso that if in 30 seconds a response is not gathered then to abort the request and try again in five minutes. Here is the code that i currently have: Imports System Imports System.IO Imports System.Net Imports System.Threading Public Class VirasecHB 'Declare the Thread Private VirasecHB_Thread As New Thread( _ New System.Threading.ThreadStart(AddressOf waitProcedure)) 'Declare Variables Dim serverID As String = "1336" Dim webURL As String = "http://www.virasec.com/intranet/tech/heart/heartdb.asp?hdur=300&hsid=" & serverID Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. WebRequestProcedure() VirasecHB_Thread.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. VirasecHB_Thread.Abort() End Sub Private Sub WebRequestProcedure() ' Create a request for the URL. Dim request As WebRequest = WebRequest.Create(webURL) ' If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials 'Sets web request timeout time request.Timeout = 30000 ' 30000 = 30 Seconds ' Get the response. Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 'Close the response request response.Close() End Sub Private Sub waitProcedure() Do 'Wait Five Mintues System.Threading.Thread.Sleep(300000) '300000 = 5 Minutes Try WebRequestProcedure() Catch ex As Exception Dim errorLog As New EventLog errorLog.WriteEntry("Virasec Heartbeat Service " & _ "has encountered an error while trying to " & _ "connect to the Virasec web server. " & _ "The service will attempt to " & _ "connect again in five minut
You have to mention the Error you're getting to get more precise answers. I can only make a wild guess with the information you've provided.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
In most probability, the CType you're doing is throwing an error as the Reponse object returned by the GetResponse() method will be null if it can't reach the Http server. Enclose the above line in a Try..Catch block. AND, next time, post the error message or what it says, as well. SG -
Hey guys the problem that I am is this. I am writting a service that every five minutes goes out and requests a web page. And the problem that I am having is whenever I disconnect my computer for the internet, the service throws an error. I have tried implementing some error handling but I can't seem to figure it out. I would like to code somethingso that if in 30 seconds a response is not gathered then to abort the request and try again in five minutes. Here is the code that i currently have: Imports System Imports System.IO Imports System.Net Imports System.Threading Public Class VirasecHB 'Declare the Thread Private VirasecHB_Thread As New Thread( _ New System.Threading.ThreadStart(AddressOf waitProcedure)) 'Declare Variables Dim serverID As String = "1336" Dim webURL As String = "http://www.virasec.com/intranet/tech/heart/heartdb.asp?hdur=300&hsid=" & serverID Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. WebRequestProcedure() VirasecHB_Thread.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. VirasecHB_Thread.Abort() End Sub Private Sub WebRequestProcedure() ' Create a request for the URL. Dim request As WebRequest = WebRequest.Create(webURL) ' If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials 'Sets web request timeout time request.Timeout = 30000 ' 30000 = 30 Seconds ' Get the response. Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 'Close the response request response.Close() End Sub Private Sub waitProcedure() Do 'Wait Five Mintues System.Threading.Thread.Sleep(300000) '300000 = 5 Minutes Try WebRequestProcedure() Catch ex As Exception Dim errorLog As New EventLog errorLog.WriteEntry("Virasec Heartbeat Service " & _ "has encountered an error while trying to " & _ "connect to the Virasec web server. " & _ "The service will attempt to " & _ "connect again in five minut
You'd have to completely redesign this. All of your code was written on the assuption that the request would work and only the occasional error would happen. Turn it around and design the service so that it assumes that every request to the site will FAIL. For instance, this:
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
'Close the response request
response.Close()always assumes that the
response
returns an object that you can close. Don't call close on an object that you don't know exists. Test for an object first, THEN call close on it if it's legitimate:Dim response As ...
If response Is Nothing Then
' The request obviously failed...
Else
response.Close()
End IfA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007