【问题标题】:WCF Service via JavaScript with XML - Array parameters通过带有 XML 的 JavaScript 的 WCF 服务 - 数组参数
【发布时间】:2012-08-20 16:02:54
【问题描述】:

我正在使用 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>

但这并没有奏效。如果有人有任何见解,我将不胜感激。

谢谢。

编辑:

取得进展。 Darin 的答案适用于原始数据类型,但我无法传递更复杂的东西,比如以下类的数组:

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>

方法被调用(我可以在调试器中验证这一点),但它传递的数组是空的。

【问题讨论】:

    标签: c# javascript xml wcf xml-serialization


    【解决方案1】:

    假设以下方法签名通过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>
    

    【讨论】:

    • 谢谢。这适用于字符串,但我实际上需要传递一个更复杂的对象,这似乎并没有像我预期的那样工作。有关详细信息,请参阅我的原始帖子。
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多