Reputation: 13
Old subject but with a twist - I searched and couldn't find answer to that.
I know I can't use optional parameters with default values with web method so I have to use function overloading but...I saw examples of one optional parameter and I have around 10!
If I understand well for 3 optional parameters I will need 7 overloaded functions (3 for 1 parameter, 3 for 2 and 1 for the whole 3) so for how many I need for 10? a lot! there must be a better way, no? and please don't tell me to use WCF - I can't switch to that now and I have to use WSDL
thanks a lot for helping
Upvotes: 1
Views: 7698
Reputation: 2213
You can declare variables as Nullable (of <your type>)
and just have one webservice with all 10 parameters
This is your Webmethod with only 2 optional parameters, but you can easily expand on this to 10:
<WebMethod(Description:="TEST1")> _
Public Function TEST1(<XmlElement()> param1 As Nullable(Of Double), <XmlElement()> param2 As Nullable(Of Double)) As <XmlElement()> Double
Try
Dim result As Double = 0
If Not param1 Is Nothing Then
result += param1
End If
If Not param2 Is Nothing Then
result += param2
End If
Return result
Catch ex As Exception
End Try
Return 0
End Function
This SoapUI call:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://mysite.org/">
<soapenv:Header/>
<soapenv:Body>
<not:TEST1>
<not:param1>1</not:param1>
<not:param2>2</not:param2>
</not:TEST1>
</soapenv:Body>
</soapenv:Envelope>
Results in this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<TEST1Response xmlns="http://mysite.org/">
<TEST1Result>3</TEST1Result>
</TEST1Response>
</soap:Body>
</soap:Envelope>
This SoapUI call:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://mysite.org/">
<soapenv:Header/>
<soapenv:Body>
<not:TEST1>
<not:param1>1</not:param1>
</not:TEST1>
</soapenv:Body>
</soapenv:Envelope>
Results in this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<TEST1Response xmlns="http://mysite.org/">
<TEST1Result>1</TEST1Result>
</TEST1Response>
</soap:Body>
</soap:Envelope>
Nullable (of Double)
in this example both parameters are optional.
Upvotes: 2
Reputation: 27342
Instead of using many optional parameters you could just pass an Object (Class) that has properties with a default value. That way it can work the same as optional parameters:
Public Class Parameters
Public Property Name As String = "Undefined"
Public Property Country as string = "United Kingdom"
End Class
Define your WebMethod to accept this object type
Public Function WebMethod(prm as Parameters)
Usage:
Pass parameters with a Name:
WebMethod(New Parameters With {.Name = "Jane"})
Pass Parameters with a Name and Country:
WebMethod(New Parameters With {.Name = "Amr", .Country = "Egypt"})
Pass Parameters with just Country:
WebMethod(New Parameters With {.Country = "China"})
Upvotes: 3