problems returning ArrayList from webServices

Discussion in 'ASP .Net Web Services' started by Mario Rodriguez, Apr 12, 2004.

  1. Hi, I'm in a big problem because my implementation of a webService that
    returns an ArrayList object have a weird behaviour. On the client side,
    instead of return an ArrayList object, returns an object[] !!!!. This is
    normal or correct ????????

    My other problem is that the ArrayList (and object[]) contains references to
    an object My Container, that is out the namespace of the webService (so I've
    to add the reference to MyContainer in both the client side and the
    webService) but on the client side, when I try read the objects from the
    object[] I got a castException due to the objectArray contains instances of
    webService.namespace.MyContainer and not of
    myContainer.namespace.MyContainer.

    I'm not sure what is happening in both situations, that if someone can give
    me a helping hand, I'll thank you a lot
     
    Mario Rodriguez, Apr 12, 2004
    #1
    1. Advertising

  2. Mario Rodriguez

    Jan Tielens Guest

    The "problem" is that an arraylist is not a strongly typed collection. If
    you take a look at the default property (Item in vb.net), the return value
    is an object. On your client side, the ArrayList will show up as an array
    (as you've noticed). Because the return value of the arraylist's default
    property was an object, they array on your client will be an array of
    objects.

    There are 2 possible (maybe more) solutions for your problem:
    1) Don't use weakly typed collections when you're using web services.
    Instead of an arraylist use a simple array for example. You can easily
    convert an arraylist to an array by using the ToArray function of the
    arraylist.

    2) Use the XmlInclude attribute on your webmethod. By doing so you can
    indicate which types you'll be expecting in your array of objects.

    --
    Greetz,
    Jan
    ________________________
    Read my weblog: http://weblogs.asp.net/jan

    "Mario Rodriguez" <> schreef in bericht
    news:...
    > Hi, I'm in a big problem because my implementation of a webService that
    > returns an ArrayList object have a weird behaviour. On the client side,
    > instead of return an ArrayList object, returns an object[] !!!!. This is
    > normal or correct ????????
    >
    > My other problem is that the ArrayList (and object[]) contains references

    to
    > an object My Container, that is out the namespace of the webService (so

    I've
    > to add the reference to MyContainer in both the client side and the
    > webService) but on the client side, when I try read the objects from the
    > object[] I got a castException due to the objectArray contains instances

    of
    > webService.namespace.MyContainer and not of
    > myContainer.namespace.MyContainer.
    >
    > I'm not sure what is happening in both situations, that if someone can

    give
    > me a helping hand, I'll thank you a lot
    >
    >
     
    Jan Tielens, Apr 13, 2004
    #2
    1. Advertising

  3. OK, I just replaced the ArrayList by an object[] and added the XmlInclude
    attribute on the web method to describe the objects contained on the array,
    it's name is MyInnerContainer. But I noticed that on the client VS.NET
    created it's own MyInnerContainer that basically is exactly the same class
    BUT have a different namespace so when I try to access the arrayElements
    throws an InvalidCastException because my method is expecting my
    MyInnerContainer and the webService is returning it's MyInnerContainer.


    Any idea how to solve this situation????


    thanks

    "Jan Tielens" <> wrote in message
    news:#k#...
    > The "problem" is that an arraylist is not a strongly typed collection. If
    > you take a look at the default property (Item in vb.net), the return value
    > is an object. On your client side, the ArrayList will show up as an array
    > (as you've noticed). Because the return value of the arraylist's default
    > property was an object, they array on your client will be an array of
    > objects.
    >
    > There are 2 possible (maybe more) solutions for your problem:
    > 1) Don't use weakly typed collections when you're using web services.
    > Instead of an arraylist use a simple array for example. You can easily
    > convert an arraylist to an array by using the ToArray function of the
    > arraylist.
    >
    > 2) Use the XmlInclude attribute on your webmethod. By doing so you can
    > indicate which types you'll be expecting in your array of objects.
    >
    > --
    > Greetz,
    > Jan
    > ________________________
    > Read my weblog: http://weblogs.asp.net/jan
    >
    > "Mario Rodriguez" <> schreef in bericht
    > news:...
    > > Hi, I'm in a big problem because my implementation of a webService that
    > > returns an ArrayList object have a weird behaviour. On the client side,
    > > instead of return an ArrayList object, returns an object[] !!!!. This

    is
    > > normal or correct ????????
    > >
    > > My other problem is that the ArrayList (and object[]) contains

    references
    > to
    > > an object My Container, that is out the namespace of the webService (so

    > I've
    > > to add the reference to MyContainer in both the client side and the
    > > webService) but on the client side, when I try read the objects from the
    > > object[] I got a castException due to the objectArray contains instances

    > of
    > > webService.namespace.MyContainer and not of
    > > myContainer.namespace.MyContainer.
    > >
    > > I'm not sure what is happening in both situations, that if someone can

    > give
    > > me a helping hand, I'll thank you a lot
    > >
    > >

    >
    >
     
    Mario Rodriguez, Apr 13, 2004
    #3
  4. You have two problems:
    - Collections are always serialized as simple arrays.
    - The general-purpose collection classes in the framework are only typed to
    work with object.

    If you return an instance of a type that will cause other types to be
    serialized polymorphically, you must use SoapInclude or XmlInclude
    attributes to include the proper runtime type information.

    If you want to serialize as a specific collection class rather than just an
    array, wrap it. So if you had a class called Boat, and a strongly-typed
    collection called BoatCollection, define your web method like:

    [WebMethod]
    public BoatRet GetBoats()
    {
    BoatRet ret = new BoatRet();
    ret.Boats.Add(...);
    ret.Boats.Add(...);
    ret.Boats.Add(...);
    return ret;
    }

    class BoatRet
    {
    BoatCollection _boats = new BoatCollection();
    public BoatCollection Boats
    {
    get { return _boats; }
    }
    }

    --
    Mickey Williams
    Author, "Microsoft Visual C# .NET Core Reference", MS Press
    www.servergeek.com


    "Mario Rodriguez" <> wrote in message
    news:...
    > Hi, I'm in a big problem because my implementation of a webService that
    > returns an ArrayList object have a weird behaviour. On the client side,
    > instead of return an ArrayList object, returns an object[] !!!!. This is
    > normal or correct ????????
    >
    > My other problem is that the ArrayList (and object[]) contains references

    to
    > an object My Container, that is out the namespace of the webService (so

    I've
    > to add the reference to MyContainer in both the client side and the
    > webService) but on the client side, when I try read the objects from the
    > object[] I got a castException due to the objectArray contains instances

    of
    > webService.namespace.MyContainer and not of
    > myContainer.namespace.MyContainer.
    >
    > I'm not sure what is happening in both situations, that if someone can

    give
    > me a helping hand, I'll thank you a lot
    >
    >
     
    Mickey Williams, Apr 13, 2004
    #4
  5. Mario Rodriguez

    JH Guest

    Mario,
    I just posted some information in response to Niklas Magnusson's
    question on Hashtables and web services that may be helpful, though
    Jan is definitely the expert here.

    (http://groups.google.ca/groups?dq=&...es&selm=)

    HTH,
    JH

    "Jan Tielens" <> wrote in message news:<#k#>...
    > The "problem" is that an arraylist is not a strongly typed collection. If
    > you take a look at the default property (Item in vb.net), the return value
    > is an object. On your client side, the ArrayList will show up as an array
    > (as you've noticed). Because the return value of the arraylist's default
    > property was an object, they array on your client will be an array of
    > objects.
    >
    > There are 2 possible (maybe more) solutions for your problem:
    > 1) Don't use weakly typed collections when you're using web services.
    > Instead of an arraylist use a simple array for example. You can easily
    > convert an arraylist to an array by using the ToArray function of the
    > arraylist.
    >
    > 2) Use the XmlInclude attribute on your webmethod. By doing so you can
    > indicate which types you'll be expecting in your array of objects.
    >
    > --
    > Greetz,
    > Jan
    > ________________________
    > Read my weblog: http://weblogs.asp.net/jan
    >
    > "Mario Rodriguez" <> schreef in bericht
    > news:...
    > > Hi, I'm in a big problem because my implementation of a webService that
    > > returns an ArrayList object have a weird behaviour. On the client side,
    > > instead of return an ArrayList object, returns an object[] !!!!. This is
    > > normal or correct ????????
    > >
    > > My other problem is that the ArrayList (and object[]) contains references

    > to
    > > an object My Container, that is out the namespace of the webService (so

    > I've
    > > to add the reference to MyContainer in both the client side and the
    > > webService) but on the client side, when I try read the objects from the
    > > object[] I got a castException due to the objectArray contains instances

    > of
    > > webService.namespace.MyContainer and not of
    > > myContainer.namespace.MyContainer.
    > >
    > > I'm not sure what is happening in both situations, that if someone can

    > give
    > > me a helping hand, I'll thank you a lot
    > >
    > >
     
    JH, Apr 13, 2004
    #5
  6. Mario Rodriguez

    Jan Tielens Guest

    An object array is also not strongly typed, is it an option for you to use
    an array of a specific class? For example Customer[] ? This will prevent
    having to use the XMLInclude attribute.

    --
    Greetz,
    Jan
    ________________________
    Read my weblog: http://weblogs.asp.net/jan

    "Mario Rodriguez" <> schreef in bericht
    news:...
    > OK, I just replaced the ArrayList by an object[] and added the XmlInclude
    > attribute on the web method to describe the objects contained on the

    array,
    > it's name is MyInnerContainer. But I noticed that on the client VS.NET
    > created it's own MyInnerContainer that basically is exactly the same class
    > BUT have a different namespace so when I try to access the arrayElements
    > throws an InvalidCastException because my method is expecting my
    > MyInnerContainer and the webService is returning it's MyInnerContainer.
    >
    >
    > Any idea how to solve this situation????
    >
    >
    > thanks
    >
    > "Jan Tielens" <> wrote in message
    > news:#k#...
    > > The "problem" is that an arraylist is not a strongly typed collection.

    If
    > > you take a look at the default property (Item in vb.net), the return

    value
    > > is an object. On your client side, the ArrayList will show up as an

    array
    > > (as you've noticed). Because the return value of the arraylist's default
    > > property was an object, they array on your client will be an array of
    > > objects.
    > >
    > > There are 2 possible (maybe more) solutions for your problem:
    > > 1) Don't use weakly typed collections when you're using web services.
    > > Instead of an arraylist use a simple array for example. You can easily
    > > convert an arraylist to an array by using the ToArray function of the
    > > arraylist.
    > >
    > > 2) Use the XmlInclude attribute on your webmethod. By doing so you can
    > > indicate which types you'll be expecting in your array of objects.
    > >
    > > --
    > > Greetz,
    > > Jan
    > > ________________________
    > > Read my weblog: http://weblogs.asp.net/jan
    > >
    > > "Mario Rodriguez" <> schreef in bericht
    > > news:...
    > > > Hi, I'm in a big problem because my implementation of a webService

    that
    > > > returns an ArrayList object have a weird behaviour. On the client

    side,
    > > > instead of return an ArrayList object, returns an object[] !!!!. This

    > is
    > > > normal or correct ????????
    > > >
    > > > My other problem is that the ArrayList (and object[]) contains

    > references
    > > to
    > > > an object My Container, that is out the namespace of the webService

    (so
    > > I've
    > > > to add the reference to MyContainer in both the client side and the
    > > > webService) but on the client side, when I try read the objects from

    the
    > > > object[] I got a castException due to the objectArray contains

    instances
    > > of
    > > > webService.namespace.MyContainer and not of
    > > > myContainer.namespace.MyContainer.
    > > >
    > > > I'm not sure what is happening in both situations, that if someone can

    > > give
    > > > me a helping hand, I'll thank you a lot
    > > >
    > > >

    > >
    > >

    >
    >
     
    Jan Tielens, Apr 15, 2004
    #6
    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. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,794
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,486
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,441
  4. Philipp
    Replies:
    6
    Views:
    971
    Arne Vajhøj
    May 28, 2008
  5. Ashish Saxena via .NET 247

    Returning ArrayList from WebServices

    Ashish Saxena via .NET 247, Apr 8, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    136
    Krishna
    Apr 11, 2005
Loading...

Share This Page