I have a web method with multiple parameters. The web method is only dependent on 2 fields, the rest are all optional.
我有一个带有多个参数的web方法。 Web方法仅依赖于2个字段,其余字段都是可选的。
[OperationContract]
public string WarehouseContactInformation(int WAID (Required), string CN (Required), string CT (Optional), string CC (Optional), string CFN (Optional), string CD (Optional), string CE (Optional),string CW (Optional))
How to I declare these parameters as optional so that when I call the Web Method I only have to pass through the fields that i have values for, example:
如何将这些参数声明为可选参数,以便在调用Web方法时,我只需要通过我有值的字段,例如:
WarehouseContactInformation(1,'Bill','00012311')
WarehouseContactInformation(1,'Bill','00012311','12415415','123525')
2 个解决方案
#1
10
You can't. Web methods doesn't support optional parameters. When you generate proxi for web method, you make get the specific signature, according to which you client and server would exchange the messages. But it can't pass the optional parameters. You can use default parameters on the server side, but no optional.
你不能。 Web方法不支持可选参数。当您为Web方法生成proxi时,您将获得特定签名,客户端和服务器将根据该签名交换消息。但它无法传递可选参数。您可以在服务器端使用默认参数,但不能使用可选参数。
#2
1
What i did is: send the parameter binded with XML, and don't bind the values of optional parameters leave that blank.
我所做的是:发送带有XML的绑定参数,并且不绑定可选参数的值,留空。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(
"<registration>" +
"<field1>" + value + "</field1>" +
"<field2>" + value(or leave blank) + "</field2>" +
"<field3>" + value + "</field3>" +
"<field4>" + value + "</field4>" +
"</registration>");
int status = objectOfService.methodName(xmlDoc);
and in web service you can do like
在Web服务中你可以这样做
public int UpdateUser(XmlNode node)
{
String filed1Value=node["field1"].InnerText;
}
Hope it helps.
希望能帮助到你。
#1
10
You can't. Web methods doesn't support optional parameters. When you generate proxi for web method, you make get the specific signature, according to which you client and server would exchange the messages. But it can't pass the optional parameters. You can use default parameters on the server side, but no optional.
你不能。 Web方法不支持可选参数。当您为Web方法生成proxi时,您将获得特定签名,客户端和服务器将根据该签名交换消息。但它无法传递可选参数。您可以在服务器端使用默认参数,但不能使用可选参数。
#2
1
What i did is: send the parameter binded with XML, and don't bind the values of optional parameters leave that blank.
我所做的是:发送带有XML的绑定参数,并且不绑定可选参数的值,留空。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(
"<registration>" +
"<field1>" + value + "</field1>" +
"<field2>" + value(or leave blank) + "</field2>" +
"<field3>" + value + "</field3>" +
"<field4>" + value + "</field4>" +
"</registration>");
int status = objectOfService.methodName(xmlDoc);
and in web service you can do like
在Web服务中你可以这样做
public int UpdateUser(XmlNode node)
{
String filed1Value=node["field1"].InnerText;
}
Hope it helps.
希望能帮助到你。