带有可选参数的 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?
谢谢ю
推荐答案
您可以通过重载和使用 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 v1.1
不允许重载
if you haven't done so already, as WS-I Basic Profile v1.1
does not allow overloads
这篇关于带有可选参数的 Web 服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!