Sunday, March 11, 2012

Raising Events across n-tiers

I'm not sure exactly what you mean by a web service in your DAL. If it's a web service, it should be accessible to your callbacks, no matter where it's logically located. Could you clarify that a bit?


I figured it out. I had to add an event handler to my code behind page. The web reference for the web service is in the DAL. It is a requirement for our purposes to be that way for scalability. The callback was stopping at the DAL and I didn't know how to send it back to the code behind layer. Basically I copied the code structure from the web-service's Reference.vb (Reference.cs for C#) to my DAL:

Public Shared Event ServiceCompleted As EventHandler

Public Delegate Sub ServiceCompletedEventHandler(ByVal sender As Object, ByVal e As Proxy.ServiceCompletedEventArgs)

Public Shared Sub BeginService(...)
Dim Proxy As New Proxy.Service
AddHandler Proxy.ServiceCompleted, AddressOf Results
Proxy.ServiceAsync(...)
End Sub

Public Shared Sub Results(ByVal sender As Object, ByVal e As Proxy.ServiceCompletedEventArgs)
RaiseEvent ServiceCompleted(e.Result, e)
End Sub

Then from the CodeBehind Layer I call the DAL like so:

Private Sub BindPage()
Dim Proxy As New DAL.Proxy
AddHandler Proxy.ServiceCompleted, AddressOf Results
End Sub

Private Sub Results(ByVal sender As Object, ByVal e As EventArgs)
Me.Result.Text = sender
End Sub

Thanks for your efforts,

Joe

No comments:

Post a Comment