代码之家  ›  专栏  ›  技术社区  ›  MgSam Brad M

WCF服务通过JavaScript与XML-数组参数

  •  0
  • MgSam Brad M  · 技术社区  · 12 年前

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

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

    1 回复  |  直到 12 年前
        1
  •  3
  •   Darin Dimitrov    12 年前

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