Namespaces

The previous example uses Sun as a namespace. All types derived from the web service will appear beneath that namespace. The namespace, and types beneath, exist for the duration of the PowerShell session.

If an attempt is made to create the web service proxy a second time using the same namespace, PowerShell can become confused.

The following example creates the web service proxy twice:

# Create once 
$sun = New-WebServiceProxy http://www.webservicex.net/sunsetriseservice.asmx?WSDL -Namespace Sun 
# Create again with the same namespace 
$sun = New-WebServiceProxy http://www.webservicex.net/sunsetriseservice.asmx?WSDL -Namespace Sun 
$latLonDate = New-Object Sun.LatLonDate 
 
$latLonDate.Latitude = 51.52377 
$latLonDate.Longitude = -0.1585369 

When the GetSunSetRiseTime method is called an error is thrown:

$sunSetRiseTime = $sun.GetSunSetRiseTime($latLonDate) 
Cannot convert argument "L", with value: "Sun.LatLonDate", for "GetSunSetRiseTime" to type "Sun.LatLonDate": "Cannot convert the "Sun.LatLonDate" value of type  
"Sun.LatLonDate" to type "Sun.LatLonDate"." 
At line:10 char:1 
+ $sunSetRiseTime = $sun.GetSunSetRiseTime($latLonDate) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo          : NotSpecified: (:) [], MethodException 
    + FullyQualifiedErrorId :MethodArgumentConversionInvalidCastArgument 

At this point, PowerShell has two different versions of Sun.LatLonDate. The version that is created by New-Object cannot be made suitable for the second version of the web service proxy.

There are a number of possible solutions to this problem:

  1. Restart the PowerShell session.
  2. Create a unique namespace for each web service proxy.
  3. Allow PowerShell to use a dynamic namespace.

The third option allows PowerShell to manage namespaces, it will not overlap these. Creating instances of objects will need the name of the dynamically generated namespace.

The following example retrieves the name of the namespace from the object returned by New-WebServiceProxy:

$sun = New-WebServiceProxy http://www.webservicex.net/sunsetriseservice.asmx?WSDL 
$namespace = $sun.GetType().Namespace 
$latLonDate = New-Object "$namespace.LatLonDate" 
$latLonDate.Latitude = 51.52377 
$latLonDate.Longitude = -0.1585369 
$sun.GetSunSetRiseTime($latLonDate) 

The value held in the namespace variable may never change. Assigning the value as shown previously, is sufficient to correctly create the LatLonDateobject, even if the New-WebServiceProxy command has been run more than once.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.154