WebForm + Callback

S

sandrofurlan

HI, I'm learning about WCF.
I've written a simple service and I'm trying to write a web-form
client.
On a simple console client that's all right. I've got the problem with
the asynchronous callback in the client.
When I click Button1 the client send request to the service and I
return the callback correctly:
variable 'res' contains the correct return value but not 'Label2'
I'm sure I forgot something, can anybody help me?
PS: sorry for my english

Here's the code for defult.aspx.vb
Imports System.ServiceModel

Partial Class _Default
Inherits System.Web.UI.Page

Private Sub Add_Callback(ByVal ar As IAsyncResult)
Dim Client As CalculatorClient = DirectCast(ar.AsyncState,
CalculatorClient)
Dim res As String = Client.EndAdd(ar)
Label2.text = res '<---Label2.text could non be
refresh!!!!!!!!!!!
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Client As New CalculatorClient
Label1.Text = Client.Add(CDbl(Text1.Text))
Client.Close()
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Client As New CalculatorClient
Dim ar As IAsyncResult = Client.BeginAdd(CDbl(Text2.Text), New
AsyncCallback(AddressOf Add_Callback), Client)
End Sub

End Class

Thank you
 
B

bruce barker

in prerender you need to add a wait for the async routine to complete,
so that its effects can be seen in the returned html. as you have it
now, the html is returned and the browser has rendered the page before
the async has completed. updating the servers version of the lbel is
meanless. remember each request (button click) creates a new class
instance to process the request.

-- bruce (sqlwork.com)
 
S

sandrofurlan

in prerender you need to add a wait for the async routine to complete,
so that its effects can be seen in the returned html. as you have it
now, the html is returned and the browser has rendered the page before
the async has completed. updating the servers version of the lbel is
meanless. remember each request (button click) creates a new class
instance to process the request.

-- bruce (sqlwork.com)

sandrofurlanwrote:

It works fine! Thanks Bruce,
I post the code
bye

Imports System.ServiceModel
Imports System.Threading

Partial Class _Default
Inherits System.Web.UI.Page

Private WaitForResponse As New ManualResetEvent(False)

' callback method
Private Sub Add_Callback(ByVal ar As IAsyncResult)
' create a new instance of client
Dim Client As CalculatorClient = DirectCast(ar.AsyncState,
CalculatorClient)
' get the result operation
Dim res As String = Client.EndAdd(ar)
' store it
Label2.Text = res
' signal event to unlock page
WaitForResponse.Set()
End Sub

' sync method: when the service is down it doesn't work!!!!!
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Client As New CalculatorClient
Label1.Text = Client.Add(CDbl(Text1.Text))
Client.Close()
End Sub

' async method
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
'create a new instance of client
Dim Client As New CalculatorClient
'call aync method
Dim ar As IAsyncResult = Client.BeginAdd(CDbl(Text2.Text), New
AsyncCallback(AddressOf Add_Callback), Client)
End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender
If IsPostBack Then
Try
'wait for the async end method
If Not
WaitForResponse.WaitOne(TimeSpan.FromMilliseconds(1000), True) Then
Throw New TimeoutException()
Catch ex As TimeoutException
Response.Write(ex.Message)
End Try
End If
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top