带有可选参数的 Web 服务方法
我已经尝试四处寻找这个,但我真的找不到适合我想做的事情的正确解决方案。抱歉重复,因为我知道以前已经有人问过这个问题。我有一个 Web 服务方法,其中我想要一个可选参数,例如:
public void MyMethod(int a, int b, int c = -3)
我已经知道我不能使用可为空的参数,所以我只是尝试使用默认值。问题是,当我在不提供参数 c 的情况下调用此方法时,仍然会出现异常。有什么地方我必须指定它实际上是可选的吗?
谢谢
Possible Duplicate:
Can I have an optional parameter for an ASP.NET SOAP web service
I have tried looking around for this but I really cannot find the right solution for what I want to do. Sorry for the repetition since I know this has been asked before. I have a Web Service Method in which I want to have an optional parameter such as:
public void MyMethod(int a, int b, int c = -3)
I already know I cannot use nullable parameters, so I am just trying to use a default value. Problem is when I call this method without supplying argument c is still get an exception. Is there somewhere I have to specify it is in fact optional?
Thanksю
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过重载和使用 MessageName 属性来获取可选参数。
为此,您可能还需要更改
为(
如果您尚未这样做),因为
WS-I Basic Profile v1.1
不允许重载You can achieve optional parameters by overloads and using the MessageName property.
For this to work, you may also need to change
to
if you haven't done so already, as
WS-I Basic Profile v1.1
does not allow overloads使用方法重载:
Use methods overloading:
我之前研究过可选参数等,并且直接的 asmx Web 服务不支持这一点(使用默认生成的 WSDL)。但是,使用 WCF,您可以将数据协定中的参数标记为 IsRequired=false - 请参阅 可选参数ASP.NET Web 服务
I've looked into optional parameters etc. before, and straight asmx web services don't support this (with default generated WSDLs). With WCF however, you can mark parameters in your datacontract as IsRequired=false - see Optional parameters in ASP.NET web service