How to send up a multi-dimensional array in a property of type system.object?

Discussion in 'ASP .Net Web Services' started by psm@delcam.com, Jul 19, 2007.

  1. Guest

    I have a web service that returns Field[][]. The Field object contains
    Name (type string) and Val (type object).

    I am using it to return back a user-defined selection of properties of
    an task object, hence Val being of type object.

    This web service works fine, until I try to put another Field[][]
    (containing an array of historical values of the task object) in to
    the Val of one field, and then I get a SOAP Exception:

    Unable to cast object of type Field[][] to Field[]

    I have to say that I'm struggling to understand the error. I know that
    the web service can return Field[][] at the top level, so I don't
    understand why it can't cope with it further down the object hierachy

    I've tried all manner of XML serialization attributes, and nothing
    makes a difference.

    Any suggestions, other than wrapping the second Field[][] in another
    class, which I'd rather not do, as the historical task data is in the
    same format as the current task data, so it doesn't seem to make sense
    to have different objects for them?

    Cheers,

    Paul
    , Jul 19, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    >I have a web service that returns Field[][]. The Field object contains
    > Name (type string) and Val (type object).
    >
    > I am using it to return back a user-defined selection of properties of
    > an task object, hence Val being of type object.
    >
    > This web service works fine, until I try to put another Field[][]
    > (containing an array of historical values of the task object) in to
    > the Val of one field, and then I get a SOAP Exception:
    >
    > Unable to cast object of type Field[][] to Field[]
    >
    > I have to say that I'm struggling to understand the error. I know that
    > the web service can return Field[][] at the top level, so I don't
    > understand why it can't cope with it further down the object hierachy
    >
    > I've tried all manner of XML serialization attributes, and nothing
    > makes a difference.
    >
    > Any suggestions, other than wrapping the second Field[][] in another
    > class, which I'd rather not do, as the historical task data is in the
    > same format as the current task data, so it doesn't seem to make sense
    > to have different objects for them?


    It's really amazing that this works at all.

    Consider that you're serializing your objects into XML for some other piece
    of code to deserialize. You aren't giving that client code much information
    about what you're sending. Have you looked at the generated WSDL? The Val
    field is declared:

    <s:element minOccurs="0" maxOccurs="1" name="Val" />

    This only works at all because the type information will be included in the
    XML for the primitive types:

    <Val xsi:type="xsd:dateTime">2007-07-19T15:39:52.4875-04:00</Val>

    Which xsi:type should it use for Field[][]?

    BTW, it _does_ work if you wrap the Field[][]. Furthermore, if you don't
    want the two to be different, then you should always use the wrapped
    version. Have your webmethod return WrappedArrayOfArrayOfField. That will
    return something like this:

    <Val xsi:type="FieldWrapper">
    <Wrapped>
    <ArrayOfField>
    <Field>
    <Name>name1</Name>
    <Val xsi:type="xsd:int">1</Val>
    </Field>
    <Field>
    <Name>name2</Name>
    <Val xsi:type="xsd:string">abc</Val>
    </Field>
    </ArrayOfField>
    ...

    --
    John Saunders [MVP]
    John Saunders [MVP], Jul 19, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 19, 8:46 pm, "John Saunders [MVP]" <john.saunders at
    trizetto.com> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > It's really amazing that this works at all.
    >
    > Consider that you're serializing your objects into XML for some other piece
    > of code to deserialize. You aren't giving that client code much information
    > about what you're sending. Have you looked at the generated WSDL? The Val
    > field is declared:
    >
    > <s:element minOccurs="0" maxOccurs="1" name="Val" />
    >
    > This only works at all because the type information will be included in the
    > XML for the primitive types:
    >
    > <Val xsi:type="xsd:dateTime">2007-07-19T15:39:52.4875-04:00</Val>
    >
    > Which xsi:type should it use for Field[][]?
    >
    > BTW, it _does_ work if you wrap the Field[][]. Furthermore, if you don't
    > want the two to be different, then you should always use the wrapped
    > version. Have your webmethod return WrappedArrayOfArrayOfField. That will
    > return something like this:
    >
    > <Val xsi:type="FieldWrapper">
    > <Wrapped>
    > <ArrayOfField>
    > <Field>
    > <Name>name1</Name>
    > <Val xsi:type="xsd:int">1</Val>
    > </Field>
    > <Field>
    > <Name>name2</Name>
    > <Val xsi:type="xsd:string">abc</Val>
    > </Field>
    > </ArrayOfField>
    > ...


    That's the part that I don't get; I have looked at the WSDL (far too
    many times!), and I can see the overall result node:

    <s:element name="GetTasksResponse">
    <s:complexType>
    <s:sequence>
    <s:element minOccurs="0" maxOccurs="1"
    name="GetTasksResult"
    type="tns:ArrayOfArrayOfField" />
    </s:sequence>
    </s:complexType>
    </s:element>

    So, in answer to your question:

    > Which xsi:type should it use for Field[][]?


    I would have thought tns:ArrayOfArrayOfField would be the type it
    would serialize as. There is a node with that name in the WSDL
    already, so I don't understand why it can't utilise that?

    I've tried putting a string[][] into Val, and that blows up too, so it
    seems that it's just a problem putting a multi-dimensional (well,
    jagged) array into the Val.

    In fact, we just tried returning a jagged array from a web method that
    returns one System.object, tried returning string[][], and that
    doesn't work either, so that seems to confirm that you just can't
    return a jagged array as System.object. I'm intrigued as to why that
    is the case though.

    Cheers,

    Paul
    , Jul 23, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2Vhbg==?=
    Replies:
    2
    Views:
    8,958
    Kevin Spencer
    Feb 6, 2004
  2. Imran Aziz
    Replies:
    4
    Views:
    8,693
    ljenner01
    Jan 19, 2011
  3. Kevin
    Replies:
    10
    Views:
    1,225
    Chris Uppal
    Sep 15, 2005
  4. Venkat
    Replies:
    4
    Views:
    970
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page