Overlapping services

When testing a SOAP interface, it is easy to get into a situation where New-WebServiceProxy has been called several times against the same web service. This can be problematic if using the Namespace parameter.

Consider the following example, which uses two instances of the web service:

$params = @{
Uri = 'http://localhost:62369/Service.asmx'
Namespace = 'SOAP'
}
# Original version
$service = New-WebServiceProxy @params
# New version
$service = New-WebServiceProxy @params

$searchConditions = @(
[SOAP.SearchCondition]@{
PropertyName = 'Symbol'
Operator = 'eq'
Value = 'H'
}
)

In theory, there is nothing wrong with this example. In practice, the SOAP.SearchCondition object is created based on the original version of the service created using New-WebServiceProxy. The method is, on the other hand, executing against the newer version.

As the method being called and the type being used are in different assemblies, an error is shown; this is repeated in the following:

PS> $service.SearchElements($searchConditions)
Cannot convert argument "searchConditions", with value: "System.Object[]", for "SearchElements" to type
"SOAP.SearchCondition[]": "Cannot convert the "SOAP.SearchCondition" value of type "SOAP.SearchCondition" to type
"SOAP.SearchCondition"."
At line:1 char:1
+ $service.SearchElements($searchConditions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

It is still possible to access the second version of SearchCondition by searching for the type, then creating an instance of that:

$searchCondition = ($service.GetType().Module.GetTypes() |
Where-Object Name -eq 'SearchCondition')::new()

$searchCondition.PropertyName = 'Symbol'
$searchCondition.Operator = 'eq'
$searchCondition.Value = 'H'

$searchConditions = @($searchCondition)

$service.SearchElements($searchConditions)

However, it is generally better to avoid the problem by allowing New-WebServiceProxy to use a dynamic namespace. At which point, an instance of the SearchCondition may be created, as shown here:

('{0}.SearchCondition' -f $service.GetType().Namespace -as [Type])::new()
..................Content has been hidden....................

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