How to use timeout property of the proxy class in VB.NET

In some cases for your web-page client, which consume a web service, you need to specify the maximum amount of time it has to wait. The article How to create an ASP.NET web-page client for a web service in VB.NET does not cover this area. By default the timeout is 100,000 milliseconds (10 seconds).

You can specify different timeout by using the Timeout property. You should combine the property with error handling mechanisms, because if the timeout period expires without a response, an exception should be thrown, giving you the option to notify the user about the problem.  The next code lines show how you can rewrite the ASP.NET web-page client to use a timeout of five seconds:

Private Sub cmdGetData_Click(sender As Object, e As System.EventArgs)

‘ Create the proxy.

Dim Proxy As New BooksService()

 

‘ This timeout will apply to all web service method calls.

Proxy.Timeout = 5000

‘ 5,000 milliseconds is 5 seconds.

Dim Ds As DataSet = Nothing

 

Try

‘ Call the web service and get the results.

Ds = Proxy.GetBooks()

Catch Err As System.Net.WebException

If Err.Status = WebExceptionStatus.Timeout Then

LblResult.Text = “Web service timed out after 5 seconds.”

Else

LblResult.Text = “Another type of problem occurred.”

End If

End Try

 

‘ Bind the results.

If Ds IsNot Nothing Then

GridView1.DataSource = Ds.Tables(0)

GridView1.DataBind()

End If

End Sub

You can also set the timeout to -1 to indicate that your client will wait as long as it takes. This will make your web application unacceptably slow if you attempt to perform a number of operations with an unresponsive web service.