Re: Stumped - Strongly Typed Arrays question (with simplified code example)

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: JH (jason.hunt_at_noticeablydifferent.com)
Date: 03/25/04


Date: 25 Mar 2004 08:07:44 -0800

Thank you for your replies!
  I finally stumbled across the Visual Studio Magazine article
yesterday afternoon and am testing that implementation. There is a
marginal (possibly negligable) difference between what is being done
in that sample versus the sample I have provided in that there is an
class that encapsulates the custom array type.
  As for using remoting the complex type; that was an option (in the
project) early on, but executive override was instituted because the
catch phrase "web service" was used and was better known than the
catch phrase ".Net Remoting". I am sure that many other corporate
developers can understand this.
  But, in the same context of using the web service versus remoting, I
was surprised to find that classes such as ArrayList (or IList) are
not exposable through the web service interface, though it makes sense
as they are not strongly-typed.
  As a culmination of both sets of information (from the VS Mag
article, and the issues with a web service exposing members of the
IList interface), I am investigating an alternative that would
encompass both solutions.
  The problem I had with the information in the VS Mag article is
that, if the web service's definitions ever change (as they can when
the provider changes their interface, escpecially important if the
provider of the web service is a third party), you have to rebuild
your proxy class manually every time it changes. You would not likely
find this error until one of your customers calls you up and says
"Hey, this hasn't been working since last friday and we're losing big
money". <Enter panick mode here> An alternative is to leave the web
reference as a Visual Studio web reference (generated) instead of
replacing it with your own modified proxy class. If the custom array
manipulation methods (basically only add and remove, and any other
ones as needed) are also provided as a web service, or as a helper
class built in client code, it would seem reasonable that you would
find the changes in a web service at compile time when you update your
web reference, rather than later, thus saving you the possible panick
mode and the exponential increase in the cost to fix the error when it
is discovered by a customer. I like the idea of exposing the custom
array manipulation methods as a web service simply because that allows
your manipulation methods to be client-independant (your ASP, JSP,
Windows Forms client all use the same manipulation methods) and to
keep the client as thin as possible.
  So, I am going to try both the VS Mag solution as well as the one I
proposed here. I'll post my findings.

Thanks for your replies so far,
JH

"Michael Pearson" <michaelp_extrajunktoremove@televox.com> wrote in message news:<#WsOzRgEEHA.2408@TK2MSFTNGP10.phx.gbl>...
> I used this article and got the solution I was looking for.
> http://www.fawcette.com/vsm/2003_06/magazine/columns/aspnet/default.aspx
>
> I wound up putting my custom classes in a separate project, and the
> webservice and client application included references to this new project.
>
> Michael
>
> "JH" <jason.hunt@noticeablydifferent.com> wrote in message
> news:a06bd62c.0403241447.21072b9a@posting.google.com...
> > Hello All,
> > I am looking for guidance on this issue. I am certain it is not new,
> > but I haven't been able to find a solution that seems to fit right, so
> > any leads to resources or other information would be greatly
> > appreciated.
> >
> > If you have a class object, exposed by a remoting service, that
> > encapsulates a private member with a strongly-typed custom array (that
> > implements CollectionBase) type, the remoting service passes the
> > strongly typed array as a System.Array, thus not enabling access to
> > other IList methods such as Add or Remove.
> >
> > When done without a web service, the accessor method for the
> > collection in the encapsulating class can access the IList methods
> > (such as Add) directly directly, using the format
> > "ItemHolder.Items.Add( new Item() );". With the web service, the array
> > comes across as a strongly-typed array (e.g. Item[]), but that does
> > not give you the ability to use Add and the like from IList
> > (implemented in the custom strongly-typed array, exposed by the web
> > service). Casting the System.Array to an IList still does not give you
> > the ability to add the item and results in an exception, "Collection
> > was of a fixed size.", being thrown.
> >
> > If you have any suggestions they would be welcomed and greatly
> > appreciated.
> >
> > Sincerely,
> > JH
> >
> > e.g.
> > //---------- Web Service Side ----------//
> > //Simple type, exposed by the Web Service
> > public class Item
> > {
> > private string name;
> > public Item(){}
> > public string Name
> > {
> > get{ return name;}
> > set{ name = value;}
> > }
> > }
> >
> > // Strongly typed collection, exposed by the Web Service
> > public class ItemCollection : CollectionBase
> > {
> > public ActivitySetList(){}
> > public int Add( Item item )
> > {
> > return List.Add( item );
> > }
> > public void Insert( int index, Item item )
> > {
> > List.Insert( index, item );
> > }
> > public void Remove( Item item )
> > {
> > List.Remove( item );
> > }
> > public bool Contains( Item item )
> > {
> > return List.Contains( item );
> > }
> > public int IndexOf( Item item )
> > {
> > return List.IndexOf( item );
> > }
> > public void CopyTo( Item[] itemArray, int index )
> > {
> > List.CopyTo( itemArray, index );
> > }
> > public Item this[ int index ]
> > {
> > get { return ( Item )List[ index ]; }
> > set { List[ index ] = value; }
> > }
> > }
> >
> > //Encapsulating type, exposed by the web service
> > public class ItemHolder
> > {
> > private ItemCollection itemCollection = new ItemCollection();
> > public ItemHolder(){}
> > public ItemCollection Items
> > {
> > get{ return itemCollection; }
> > }
> > }
> >
> > // Webservice methods
> > [WebMethod]
> > public ItemHolder CreateItemHolder()
> > {
> > return new ItemHolder();
> > }
> > [WedMethod]
> > public Item CreateNewItem()
> > {
> > return new Item();
> > }
> >
> > //---------- Client Side ----------//
> > // Sample client code
> > IList _itemHolder = <WebReferenceName>.CreateItemHolder();
> > // The following line throws the
> > //"Colleciton was of a fixed size" exception
> > ((IList)_itemHolder.Items).Add( <WebReferenceName>.CreateItem() );



Relevant Pages

  • RE: Fire Event to Server-side
    ... Think of the web service as a receive port, ... you can have the client send a request to ... custom port listener, custom protocol, custom serializer/marshaller, custom ... Subject: Fire Event to Server-side ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Exception
    ... Catch the exception at the web service and then throw your own custom ... exception to the client that is populated with whatever information you want ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Implementing a common SOAP Header across multiple Web Service Pages
    ... to set a client up to reference multiple Web ... the Web Service site would ... Your point about leaving the ASMX page as lean as possible and acting just ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Implementing a common SOAP Header across multiple Web Service Pages
    ... between my Web Service application and the client. ... public string SID; ... Web Service page, rather than to a dozen or so separate Web Service pages ... You can easily create a .ASMX file ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Still Need desperate help to start with ASP NET - simplified problems - HELP!!
    ... You could do it as a web service. ... The handler can draw on the webservice for information and db lookup. ... IE posts data AJAX to handler on web server ... featured application (say thick client) which does a lot of complicate ...
    (microsoft.public.dotnet.framework.aspnet)