我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗 [英] Can I have an optional parameter for an ASP.NET SOAP web service
问题描述
我想用这个签名构建一个网络服务,如果 param2 为空,它不会抛出异常.这可能吗?
I want to build a webservice with this signature, which does not throw an exception if param2 is left empty. Is this possible?
[WebMethod]
public string HelloWorld(string param1, bool param2) { }
异常是 System.ArgumentException,在尝试将空字符串转换为布尔值时抛出.
The exception is a System.ArgumentException that is thrown when trying to convert the empty string to boolean.
目前尚未奏效的想法:
webservices 不允许方法重载,比如
method overloading is not allowed for webservices, like
public string HelloWorld(string param1)
{
return HelloWorld(param1, false);
}
按照此处的建议:
- 使
bool
可以为空bool?
.同样的例外. - 操作 WSDL,请参阅此答案莉>
- make
bool
nullablebool?
. Same exception. - manipulate the WSDL, see this answer
我的问题与这个问题有关,但唯一的答案是到 WCF 合约,我还没有使用过.
My question is related to this question, but the only answer points to WCF contracts, which I have not used yet.
推荐答案
您可以在具有 MessageName 属性的 Web 服务中使用重载方法.这是实现重载功能的一种变通方法.
You can have a Overloaded Method in webservices with MessageName attribute. This is a workaround to achieve the overloading functionality.
查看 http://msdn.microsoft.com/en-us/library/byxd99hx%28VS.71%29.aspx
[WebMethod(MessageName="Add3")]
public double Add(double dValueOne, double dValueTwo, double dValueThree)
{
return dValueOne + dValueTwo + dValueThree;
}
[WebMethod(MessageName="Add2")]
public int Add(double dValueOne, double dValueTwo)
{
return dValueOne + dValueTwo;
}
这些方法将作为 Add2
和 Add3
对外可见.
The methods will be made visible as Add2
and Add3
to the outside.
这篇关于我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!