vb.net中的web方法:可选参数-太多,无法重载
这是一个古老的话题,但有点曲折——我搜索了一下,却找不到答案 我知道我不能在web方法中使用带有默认值的可选参数,所以我必须使用函数重载,但是…我看到了一个可选参数的示例,我有大约10个 如果我能很好地理解3个可选参数,我需要7个重载函数,3个用于1个参数,3个用于2个参数,1个用于整个3个参数,那么10个参数需要多少个重载函数呢?很多一定有更好的办法,不是吗?请不要告诉我使用WCF——我现在不能切换到那个,我必须使用WSDLvb.net中的web方法:可选参数-太多,无法重载,.net,vb.net,web-services,overloading,optional,.net,Vb.net,Web Services,Overloading,Optional,这是一个古老的话题,但有点曲折——我搜索了一下,却找不到答案 我知道我不能在web方法中使用带有默认值的可选参数,所以我必须使用函数重载,但是…我看到了一个可选参数的示例,我有大约10个 如果我能很好地理解3个可选参数,我需要7个重载函数,3个用于1个参数,3个用于2个参数,1个用于整个3个参数,那么10个参数需要多少个重载函数呢?很多一定有更好的办法,不是吗?请不要告诉我使用WCF——我现在不能切换到那个,我必须使用WSDL 非常感谢您的帮助您不必使用许多可选参数,只需传递具有默认值属性的对象
非常感谢您的帮助您不必使用许多可选参数,只需传递具有默认值属性的对象类即可。这样,其工作原理与可选参数相同:
Public Class Parameters
Public Property Name As String = "Undefined"
Public Property Country as string = "United Kingdom"
End Class
定义WebMethod以接受此对象类型
Public Function WebMethod(prm as Parameters)
用法:
使用名称传递参数:
WebMethod(New Parameters With {.Name = "Jane"})
使用名称和国家/地区传递参数:
WebMethod(New Parameters With {.Name = "Amr", .Country = "Egypt"})
仅通过国家/地区传递参数:
WebMethod(New Parameters With {.Country = "China"})
您不必使用许多可选参数,只需传递具有默认值属性的对象类即可。这样,其工作原理与可选参数相同:
Public Class Parameters
Public Property Name As String = "Undefined"
Public Property Country as string = "United Kingdom"
End Class
定义WebMethod以接受此对象类型
Public Function WebMethod(prm as Parameters)
用法:
使用名称传递参数:
WebMethod(New Parameters With {.Name = "Jane"})
使用名称和国家/地区传递参数:
WebMethod(New Parameters With {.Name = "Amr", .Country = "Egypt"})
仅通过国家/地区传递参数:
WebMethod(New Parameters With {.Country = "China"})
您可以将变量声明为可为null的,并且只有一个包含所有10个参数的webservice 这是您的Webmethod,只有2个可选参数,但您可以轻松地将其扩展到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
此SoapUI调用:
<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>
<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>
结果如下:
<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>
<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>
此SoapUI调用:
<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>
<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>
结果如下:
<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>
<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>
在本例中,两个参数都是可选的。您可以将变量声明为可为null的,并且只有一个Web服务包含所有10个参数 这是您的Webmethod,只有2个可选参数,但您可以轻松地将其扩展到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
此SoapUI调用:
<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>
<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>
结果如下:
<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>
<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>
此SoapUI调用:
<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>
<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>
结果如下:
<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>
<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>
在本例中,两个参数都是可选的。这是很多可选参数!有时什么都不传递是好的。这是很多可选参数!有时一无所获是件好事。非常感谢你的回答。我承认我是WEB领域的初学者,所以我想澄清一下——我的客户是一个WEB站点,他将不得不接受这些参数并创建这个对象类?这对他来说不是问题吗?非常感谢你的回答。我承认我是WEB领域的初学者,所以我想澄清一下——我的客户是一个WEB站点,他将不得不接受这些参数并创建这个对象类?这对他来说不是问题吗?