Wednesday, March 5, 2008

Creating a One-Way Webservice to improve responsiveness

By default a call to a webmethod will wait for the webservice method to complete the whole webmethod, even if it does not return a value. Now you could invoke the method Asynchronously, but considering you don't care what comes back, because nothing its gonna come back why bother? Well let's see how you can make the method one way, so processing resumes on the client straight after the call to the method. Consider the following:

<SoapDocumentMethod(Oneway:=True)> _
<WebMethod()> _
Public Sub Oneway()
Threading.Thread.Sleep(10000)
End Sub

<WebMethod()> _
Public Sub Twoway()
Threading.Thread.Sleep(10000)
End Sub

And then we would call theses like this:

Dim proxy As New localhost.Service()
Dim dtStart As New Date
dtStart = Now
proxy.Oneway()
MessageBox.Show(DateDiff(DateInterval.Second, dtStart, Now))
dtStart = Now
proxy.Twoway()
MessageBox.Show(DateDiff(DateInterval.Second, dtStart, Now))

When you run the code the one way method returns instantly, no delay, the two way will wait 10 seconds and stall the client app!!

No comments: