Charles Kierklo
1/5/2003 1:12:00 AM
I am trying to create an Asynchronous Web service that has two methods.
One to perform some work (GetPrice), and one to return the status of that
work.
Like the following:
<WebMethod(Description:="Get the price of a book based on its ProductID
number.", EnableSession:=True)> _
Public Function GetPrice(ByVal ProductID As String) As Double
' Randomly make this routine take between 1 and 10 seconds.
Dim r As New Random()
Session("PercentComplete") = 2
Thread.Sleep(r.Next(1000, 10000))
Return dbProducts.GetPrice(ProductID)
End Function
<WebMethod(Description:="", EnableSession:=True)> Public Function
PercentComplete() As Integer
Return Session("PercentComplete")
End Function
I call it from an ASPX page like the following:
' Create an instance of the web service object
Dim Products As New localhost.Service1()
Dim l_intPercentComplete As Integer
' Call the method asynchronously
Dim result As IAsyncResult = Products.BeginGetPrice("1", Nothing,
Nothing)
Do
' Wait up to 5 seconds for the method to complete
result.AsyncWaitHandle.WaitOne(New TimeSpan(0, 0, 5), False)
' Did the service respond in time?
If (result.IsCompleted) Then
' Yes
Response.Write("The vendor has this item for $" &
Products.EndGetPrice(result).ToString() & ".<BR>")
Exit Do
Else
' No
l_intPercentComplete = Products.PercentComplete()
Response.Write("Percent Complete: " & l_intPercentComplete &
"<BR>")
End If
Loop
The problem is that the PercentComplete method always returns 0, because the
call creates a new instance of the Web Service.
How can I make the call on the same instance?