我正在使用 JavaScript 通过 XML 与 WCF 服务进行通信(我无法使用 JSON)。到目前为止,对于公开“原始”数据类型参数的 WCF 方法来说,这一直运行良好,但现在我需要调用接受数组的 WCF 方法。我一直无法弄清楚如何正确调整我的 XML。
例如,具有两个参数的 WCF 方法接受:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<MySimpleMethod xmlns="http://tempuri.org/">
<parameter1>value</parameter1>
<parameter2>someOtherValue</parameter2>
</MySimpleMethod>
</s:Body>
</s:Envelope>
我认为我可以通过执行以下操作来传递一个数组(在本例中为字符串):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<MyMethodWithAnArrayParameter xmlns="http://tempuri.org/">
<arrayParameterName>value1</arrayParameterName>
<arrayParameterName>value2</arrayParameterName>
</MyMethodWithAnArrayParameter>
</s:Body>
</s:Envelope>
但这并没有奏效。如果有人有任何见解,我将非常感激。
谢谢。
编辑:
取得进展。达林的答案适用于原始数据类型,但我无法传递更复杂的东西,例如以下类的数组:
public class Address
{
public String Number {get; set;};
public String City {get; set;};
public String State {get; set;};
}
我已经尝试过这个:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<TestMethod xmlns="http://tempuri.org/">
<args xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Address>
<Number>31</Number>
<City>Houston</City>
<State>Texas</State>
</a:Address>
</args>
</TestMethod>
</s:Body>
</s:Envelope>
该方法被调用(我可以在调试器中验证这一点),但它传递的数组是空的。
最佳答案
假设通过 basicHttpBinding
公开以下方法签名:
[OperationContract]
string MyMethodWithAnArrayParameter(string[] values, string parameter2);
你可以这样调用:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<MyMethodWithAnArrayParameter xmlns="http://tempuri.org/">
<values xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:string>value1</a:string>
<a:string>value2</a:string>
</values>
<parameter2>param2</parameter2>
</MyMethodWithAnArrayParameter>
</s:Body>
</s:Envelope>
<小时/>
更新:
假设以下操作合约:
[OperationContract]
string TestMethod(Address[] args);
请求可能如下所示
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<TestMethod xmlns="http://tempuri.org/">
<args xmlns:a="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Address>
<a:City>Houston</a:City>
<a:Number>31</a:Number>
<a:State>Texas</a:State>
</a:Address>
<a:Address>
<a:City>Washington</a:City>
<a:Number>21</a:Number>
<a:State>DC</a:State>
</a:Address>
</args>
</TestMethod>
</s:Body>
</s:Envelope>
关于c# - 通过带有 XML 的 JavaScript 的 WCF 服务 - 数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041130/