Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Powershell to call asynchronous web methods on a web service

I'm using PowerShell V2 and trying to find an example of using a web service proxy to call an asynchronous web method:

Here is the code I have so far:

$Uri = "http://localhost/mywebservice.asmx?wsdl" 

$proxy = New-WebServiceProxy -Uri $Uri -UseDefaultCredential

The web services has the following methods BeginFoo EndFoo FooAsync *FooCompleted*

Hopefully this makes sense

like image 642
Jason Horner Avatar asked Nov 24 '25 17:11

Jason Horner


1 Answers

Here's an example of using BeginInvoke/EndInvoke. Run $ar | Get-Member to see what other methods and properties are available to you on the IAsyncResult object.

PS> $zip = New-WebServiceProxy -uri http://www.webservicex.net/uszip.asmx?WSDL
PS> $ar = $zip.BeginGetInfoByAreaCode("970", $null, $null)

... other PS script ...

# Now join the async work back to the PowerShell thread, wait for completion
# and grab the result. WaitOne returns false on timeout, true if signaled.
PS> $ar.AsyncWaitHandle.WaitOne([timespan]'0:0:5')
True
PS> $ar.IsCompleted
True
PS> $res = $zip.EndGetInfoByAreaCode($ar)
PS> $res.Table


CITY      : Whitewater
STATE     : CO
ZIP       : 81527
AREA_CODE : 970
TIME_ZONE : M

CITY      : Wiggins
STATE     : CO
ZIP       : 80654
AREA_CODE : 970
TIME_ZONE : M
like image 195
Keith Hill Avatar answered Nov 28 '25 17:11

Keith Hill