0

I'm using JavaScript to communicate to a WCF service via XML (I can't use JSON). This has been working well so far for WCF methods which expose arguments of "primitive" data types, but now I need to call a WCF method which accepts an array. I've been unable to figure out how to tweak my XML properly.

For example, a WCF method with two parameters accepts:

<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>

I thought I might be able to pass an array (of strings in this case) by doing the following:

<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>

But this hasn't worked. If anyone has any insight I'd be very appreciative.

Thanks.

EDIT:

Making progress. Darin's answer works for primitive data types, but I'm unable to pass something more complex, say an array of the following class:

public class Address 
{
    public String Number {get; set;};
    public String City {get; set;};
    public String State {get; set;};
}

I've tried this:

<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>

The method gets called (I can verify this in a debugger), but the array that it gets passed is empty.

1 Answer 1

3

Assuming the following method signature exposed over a basicHttpBinding:

[OperationContract]
string MyMethodWithAnArrayParameter(string[] values, string parameter2);

you could invoke like this:

<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>

UPDATE:

Assuming the following operation contract:

[OperationContract]
string TestMethod(Address[] args);

The request could look like this

<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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This worked for strings, but I actually need to pass a more complex object, which doesn't seem to be working as I expected. See my original post for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.