Re: problems returning ArrayList from webServices

From: Mickey Williams (my)
Date: 04/13/04


Date: Tue, 13 Apr 2004 09:24:04 -0700

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" <mrodriguez@avantica.net> wrote in message
news:eaEbN9MIEHA.2144@TK2MSFTNGP12.phx.gbl...
> 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
>
>


Relevant Pages

  • Re: Serialization, RMI and deep copy
    ... However you don't have to *explicitly* serialize anything at all, RMI ... > vs what the store now contains). ... > a client of the store and can insert configuration changes. ...
    (comp.lang.java.programmer)
  • Re: Serializing XmlDocument to SerializationInfo
    ... Are you worried about the size of data being transmitted to/from a remoting client? ... If you are using binary serialization over Tcp that will help to decrease the size of messages transmitted to/from a remoting ... Just add the stream reference to the SerializationInfo with a custom key like, ... and serialize a path string. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: problem in InvalidCastException-Return argument has an invalid type
    ... DataSet is always serialized to XML. ... DataSet is not shared between the client and server. ... always serialized to XML, so you can just use WriteXml to serialize it ...
    (microsoft.public.dotnet.framework.remoting)
  • RE: Updating DB with large collections
    ... Large sub-objects –that is an object which is contained in the main object ... this seems like a complex solution to what I imagined should be quite common ... These entities are being sent to the client - where the user might ... I've tried to serialize my arraylists - both ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Convert a dataset to an array
    ... I'm not trying to serialize. ... I'm trying to load data into a component. ... component accepts 1D arrays. ... StringBuilderVal.Append.Append(blah blah blah Yuck!!! ...
    (microsoft.public.dotnet.languages.csharp)