在 C# 中使用可选参数从 Web 引用调用 WebMethod
我使用 .Net Webservices 创建了一个带有 2 个可选参数的虚拟 Web 服务,但是在实际产品中,我们将有更多可选参数(想想:查询过滤器)。问题在于,在调用 Web 服务时不可能省略可选参数,这意味着在针对实际 Web 服务进行开发时,每次调用 Web 服务时都可能存在数十个 NULL 值。
现在,这个虚拟 Web 服务仅包含此内容,以支持 2 个可选字符串参数:
[WebMethod]
public string HelloWorld(String PARAM_1="", String PARAM_2="")
{ return ""; }
在 WSDL 中,它确实使用最小值 0 和最大值 1 显示这些参数:
<s:element minOccurs="0" maxOccurs="1" name="PARAM_1" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="PARAM_2" type="s:string"/>
问题出在此处:当通过 Visual Studio 2010 中的 Web 引用添加此 Web 服务时,它确实会使用 HelloWorld 方法创建服务对象。但是它没有考虑可选参数。它创建一个没有可选参数的函数 HelloWorld。它总是需要 PARAM_1 和 PARAM_2,因此无论我们实际想要使用哪个参数,可选参数都必须填充 NULL。
VS2010 从 Web 参考中生成的定义是这样的,显示了这个问题:
public string HelloWorld(string PARAM_1, string PARAM_2) {
object[] results = this.Invoke("HelloWorld", new object[] { PARAM_1, PARAM_2});
是否有一种方法可以在 VS2010 中使用带有可选参数的 Web 服务,而不必在每次调用时强制将潜在的数十个可选参数设为 NULL?我们希望继续使用 Visual Studio 中方便的网络引用,而不是一直被迫这样做。
I have created a dummy web service with 2 optional parameters using the .Net Webservices, however in the real product we are going to have a lot more optional parameters(think: filters for a query). The problem is that it is not possible to leave out the optional parameters when calling the webservice, meaning there will be a potential dozens of NULL values in every call to the webservice when developing against the real webservice.
Right now this dummy webservice consists of only this, to support 2 optional string parameters:
[WebMethod]
public string HelloWorld(String PARAM_1="", String PARAM_2="")
{ return ""; }
In the WSDL it indeed displays these parameters with minvalue 0 and maxvalue 1:
<s:element minOccurs="0" maxOccurs="1" name="PARAM_1" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="PARAM_2" type="s:string"/>
The problem lies here: When adding this webservice through a web reference in Visual Studio 2010 it will indeed create the service object with the HelloWorld Method. However it does not take into account the optional parameters. It creates a function HelloWorld with no optional parameters. It always requires both PARAM_1 and PARAM_2, so the optional parameters have to be filled with NULLs regardless of which ones we actually would want to use.
The definition VS2010 generates out of the web reference is this, showing the issue:
public string HelloWorld(string PARAM_1, string PARAM_2) {
object[] results = this.Invoke("HelloWorld", new object[] { PARAM_1, PARAM_2});
Is there a way to use webservices with optional parameters in VS2010 without being forced to NULL a potential dozens of optional parameters with every call? We would like to keep using the convenient webreferences in Visual Studio without being forced to do this all the time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想重构方法来接受具有十几个可为空属性(而不是十几个参数)的结构是有意义的。
I guess refactor method to accept structure with dozen of nullable properties (instead of dozen parameters) will make sence.
如果将所有参数包装在单独的类中,则可以创建可选参数。这代表一条新消息,您可以自行决定将某些属性设为可选。您还可以在分部类声明中为服务生成的类提供不同的构造函数。
在我看来,这是比方法重载更好的解决方案,方法重载甚至不适用于 Web 服务。您必须为每个重载创建一个稍微不同的新方法。
You can create optional parameters if you wrap all parameters in a separate class. This represents a new message where you can decide on your own to make certain properties optional. You can also provide different constructors in a partial class declaration to the service generated classes.
This is IMO a better solution than method overloading, which doesn't even work with web services. You have to create a new and slightly different method for each overload.
minOccurs = 0 指的是变量的出现次数。因为它的类型为 string minOccurs = 0,因为它有一个默认值。
您可以尝试使用不同类型的可选参数并检查 wsdl.
The minOccurs = 0 refers to the occurance of the variable. Since its of type string minOccurs = 0 since it would have a default value.
Can you try to use optional parameter with a different type and check the wsdl.
可选参数是特定于语言的构造。基于 SOAP 的 Web 服务对可选参数一无所知,因为基于 SOAP 的 Web 服务与语言无关。
如果您希望为服务调用提供可选参数,并且希望获得可选参数的客户端感觉,则需要使用自己的代码重写生成的存根方法。
Optional parameters are a language-specific construct. SOAP-based web services know nothing about optional parameters, because SOAP-based web services are language agnostic.
If you want to have an optional parameter for the service call, you'll need to override the generated stub method with your own code if you want the client-side feel of optional parameters.