problems returning ArrayList from webServices

M

Mario Rodriguez

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
 
J

Jan Tielens

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

Mario Rodriguez

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 said:
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 said:
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
 
M

Mickey Williams

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; }
}
}
 
J

JH

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&[email protected])

HTH,
JH

Jan Tielens said:
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 said:
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
 
J

Jan Tielens

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top