vb.net 中的 web 方法:可选参数 - 太多重载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16839091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:48:09  来源:igfitidea点击:

web method in vb.net: optional parameters - too many for overloading

.netvb.netweb-servicesoverloadingoptional

提问by user2366211

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!

我知道我不能在 web 方法中使用带有默认值的可选参数,所以我必须使用函数重载但是......我看到了一个可选参数的示例,我有大约 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

如果我对 3 个可选参数理解得很好,我将需要 7 个重载函数(1 个参数为 3 个,2 个为 3 个,整个 3 个为 1 个),那么 10 个需要多少个?很多!一定有更好的方法,不是吗?请不要告诉我使用 WCF - 我现在无法切换到,我必须使用 WSDL

thanks a lot for helping

非常感谢您的帮助

回答by Matt Wilko

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

定义您的 WebMethod 以接受此对象类型

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:

仅使用 Country 传递参数:

WebMethod(New Parameters With {.Country = "China"})

回答by George

You can declare variables as Nullable (of <your type>)and just have one webservice with all 10 parameters

您可以将变量声明为,Nullable (of <your type>)并且只有一个包含所有 10 个参数的网络服务

This is your Webmethod with only 2 optional parameters, but you can easily expand on this to 10:

这是只有 2 个可选参数的 Webmethod,但您可以轻松地将其扩展到 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:

这个 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>

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:

这个 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: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.

Nullable (of Double)在这个例子中,两个参数都是可选的。