使用可选参数的Web服务方法 [英] Web Service Method with Optional Parameters
问题描述
可能重复:结果
的我能有一个ASP.NET SOAP网络服务的
我试图四处寻找,但我真的找不到什么我想要做的正确的解决方案。很抱歉的重复,因为我知道这已经问过。我有一个Web服务方法中,我想有一个可选的参数,如:
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)
我已经知道我不能用空的参数的,所以我只是试图使用一个默认值。问题是,当我把这种方法不提供的参数C是仍然得到一个异常。有没有什么地方我必须指定它实际上是可选的?
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ю
推荐答案
您可以实现通过过载和使用MessageName属性可选参数。
You can achieve optional parameters by overloads and using the MessageName property.
[WebMethod(MessageName = "MyMethodDefault")]
public void MyMethod(int a, int b)
{
MyMethod( a, b, -3);
}
[WebMethod(MessageName = "MyMethod")]
public void MyMethod(int a, int b, int c)
对于这个工作,你可能还需要修改
For this to work, you may also need to change
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
到
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
如果您还没有这样做的话,因为 WS-I Basic Profile的1.1版
不允许超载
if you haven't done so already, as WS-I Basic Profile v1.1
does not allow overloads
这篇关于使用可选参数的Web服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!