C# 我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1723052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I have an optional parameter for an ASP.NET SOAP web service
提问by Hauke
I want to build a webservice with this signature, which does not throw an exception if param2 is left empty. Is this possible?
我想用这个签名构建一个 web 服务,如果 param2 为空,它不会抛出异常。这可能吗?
[WebMethod]
public string HelloWorld(string param1, bool param2) { }
The exception is a System.ArgumentException that is thrown when trying to convert the empty string to boolean.
异常是 System.ArgumentException,它在尝试将空字符串转换为布尔值时抛出。
Ideas that have not worked so far:
到目前为止还没有奏效的想法:
method overloading is not allowed for webservices, like
public string HelloWorld(string param1) { return HelloWorld(param1, false); }
网络服务不允许方法重载,例如
public string HelloWorld(string param1) { return HelloWorld(param1, false); }
as suggested here:
正如这里所建议的:
- make
bool
nullablebool?
. Same exception. - manipulate the WSDL, see this answer
- 使
bool
可空bool?
。同样的例外。 - 操作 WSDL,请参阅此答案
My question is related to this question, but the only answer points to WCF contracts, which I have not used yet.
我的问题与这个问题有关,但唯一的答案指向 WCF 合同,我还没有使用过。
采纳答案by Rasik Jain
You can have a Overloaded Method in webservices with MessageName attribute. This is a workaround to achieve the overloading functionality.
您可以在具有 MessageName 属性的 Web 服务中使用重载方法。这是实现重载功能的一种变通方法。
Look at http://msdn.microsoft.com/en-us/library/byxd99hx%28VS.71%29.aspx
查看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;
}
The methods will be made visible as Add2
and Add3
to the outside.
该方法将在可见的Add2
和Add3
到外面。
回答by Chuck Conway
You can not have overloaded Web Service methods. The SOAP protocol does not support it. Rasik's code is the workaround.
您不能有重载的 Web 服务方法。SOAP 协议不支持它。Rasik 的代码是解决方法。
回答by Jaleel
I know this post is little old. But I think the method names should be same for the example from Rasik.If both method names are same, then where overloading comes there. I think it should be like this..
我知道这个帖子有点旧。但是我认为 Rasik 的示例的方法名称应该相同。如果两个方法名称相同,那么重载就出现在那里。我觉得应该是这样。。
[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;
}
回答by Diego
If you REALLYhave to accomplish this, here's a sort of hackfor the specific case of a web method that has only primitive types as parameters:
如果您真的必须完成此操作,那么对于仅具有原始类型作为参数的 Web 方法的特定情况,这里有一种技巧:
[WebMethod]
public void MyMethod(double requiredParam1, int requiredParam2)
{
// Grab an optional param from the request.
string optionalParam1 = this.Context.Request["optionalParam1"];
// Grab another optional param from the request, this time a double.
double optionalParam2;
double.TryParse(this.Context.Request["optionalParam2"], out optionalParam2);
...
}
回答by Velizar Hristov
Make all optional arguments strings. If no argument is passed, the input is treated as null.
制作所有可选参数字符串。如果没有传递参数,则输入被视为空值。
回答by Alexey
In accordance with MinOccurs Attribute Binding Supportand Default Attribute Binding Support:
Value type accompanied by a public bool field that uses the Specified naming convention described previously under Translating XSD to source - minOccurs value of output
<element>
element 0.
result:[WebMethod] public SomeResult SomeMethod(bool optionalParam, [XmlIgnore] bool optionalParamSpecified)
<s:element minOccurs="0" maxOccurs="1" name="optionalParam" type="s:boolean" />
Value type with a default value specified via a System.Component.DefaultValueAttribute - minOccurs value of output
<element>
element 0. In the<element>
element, the default value is also specified via the default XML attribute.
result:[WebMethod] public SomeResult SomeMethod([DefaultValue(true)] bool optionalParam)
<s:element minOccurs="0" maxOccurs="1" default="true" name="optionalParam" type="s:boolean" />
值类型附带一个公共 bool 字段,该字段使用前面在将 XSD 转换为源 - minOccurs 输出
<element>
元素 0 的值中描述的指定命名约定。
结果:[WebMethod] public SomeResult SomeMethod(bool optionalParam, [XmlIgnore] bool optionalParamSpecified)
<s:element minOccurs="0" maxOccurs="1" name="optionalParam" type="s:boolean" />
具有通过 System.Component.DefaultValueAttribute 指定的默认值的值类型 - 输出
<element>
元素 0 的minOccurs 值。在<element>
元素中,默认值也通过默认 XML 属性指定。
结果:[WebMethod] public SomeResult SomeMethod([DefaultValue(true)] bool optionalParam)
<s:element minOccurs="0" maxOccurs="1" default="true" name="optionalParam" type="s:boolean" />