Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / September 2006

Tip: Looking for answers? Try searching our database.

InvalidCastException when casting object returned from web service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim S - 06 Sep 2006 15:16 GMT
I have a .net framework 2.0 client (Pocket PC) and a .net 2.0 webservice that
communicate on the same LAN.  The Pocket PC has no problem consuming strings
returned from the web service methoeds but I’m unable to receive and a custom
object.  The error occurs when the PocketPC app casts a returned object
(WebServiceServer.objectA) from the webservice to pocketPC.ObjectA (results
in InvalidCastExeption).  ObjectA is a simple class and is identical on the
PocketPC and Webservice.  I’ve verified the object is being returned to the
PocketPC intact (debugging indicates all correct types exist and values
populated) but when I try to cast it from type Object to pocketPC.ObjectA in
the PocketPC code I get the exception.  I’ve also replaced the representation
of pocketPC.ObjectA in Reference.cs with a copy of the actual
pocketPC.ObjectA.  What am I doing wrong?  Is this not supported by compact
framework?
Thanks,
Jim

public class ObjectA //same on client and web service
   {
       private string sStringA = string.Empty;
       private string sStringB = string.Empty;
       private string sStringC = string.Empty;

       public ObjectA() { }

       public ObjectA() string sParamA, DateTime dtParamB,string sParamC)
       {
           this.sStringA = sSomeStringDeterminedByWebServiceBizLogic;
           this.sStringB = sSomeStringDeterminedByWebServiceBizLogic;;
           this.sStringC = sSomeStringDeterminedByWebServiceBizLogic;;
       }
       public string StringA
       {
           get { return this.sStringA;}
           set { this.sStringA = value; }
       }

       public string StringB
       {
           get { return this.sStringB; }
       }
       public string SringC
       {
           get { return this.sStringC; }
           set { this.sStringC = value; }
       }    
  }

Client code (Pocket PC)

public static ObjectA GetObjectAFromWebService()
       {
       WebServiceServer.Service ws = new WebServiceServer.Service();
           object tmpObject = (object)ws.GetObjectAForPocketPC(someParamA,
someParamB);
           ObjectA objectA = (ObjectA)tmpObject;
           //The above cast results in the InvalidCastException

//I’ve also tried to assign directly to the PocketPC ObjectA but receive a
compile time error.  The below statement does not work.
//Business.CredsObject credsObject = ws.GetObjectAForPocketPC (someParamA,
someParamB)
           return objectA;
       }
John Saunders - 06 Sep 2006 18:05 GMT
>I have a .net framework 2.0 client (Pocket PC) and a .net 2.0 webservice
>that
[quoted text clipped - 8 lines]
> the
> PocketPC and Webservice.

If it was identical, you wouldn't have received the InvaliCastException!

> I've verified the object is being returned to the
> PocketPC intact (debugging indicates all correct types exist and values
[quoted text clipped - 6 lines]
> compact
> framework?

It's not supported by web services at all!

Have you (or anyone else who's been asking the same question) ever seen an
example where this works? Where it's really the same class on both client
and server?  You have not seen such a thing, because it doesn't exist.

There is a big, think, opaque wall between the client and the server. There
are only a few small openings in this wall. One set of openings is the
openings through which you actually send requests and receive the responses.
Another set is in the form of the WSDL  describing the web service, and any
XML Schemas referenced by or contained in the WSDL. The WSDL and the schemas
represent the contract between the client and server. There is no other
contract, implied or explicit. This is by design.

Anything which cannot be "seen" through one of these small openings should
be ignored. You cannot, for instance, assume that a class called "Foo" on
the server bears any relationship to a class called "Foo" on the client. No
such relationship was "visible" through one of the openings. What was
visible is that (for instance), a given web service operation promised to
return a response matching the XML ComplexType called "Foo" in namespace
"urn:some.namespace". As long as the server sends you something that matches
that schema, it's kept its promise to you.

It did not promise you an instance of a class, so you shouldn't be surprised
not to receive one.

John
Jim S - 06 Sep 2006 18:16 GMT
Thanks John.  I understand now that the object itself never crosses, nor are
they "the same".  However, the ObjectA class on each side do contain the same
code (copy/paste) so shouldn't I be able to cast one to the other?

> >I have a .net framework 2.0 client (Pocket PC) and a .net 2.0 webservice
> >that
[quoted text clipped - 49 lines]
>
> John
John Saunders - 06 Sep 2006 18:20 GMT
> Thanks John.  I understand now that the object itself never crosses, nor
> are
> they "the same".  However, the ObjectA class on each side do contain the
> same
> code (copy/paste) so shouldn't I be able to cast one to the other?

No.  They're not the same thing at all.

John
Jim S - 06 Sep 2006 18:40 GMT
How would you recommend solving the problem of moving a .net object between
systems via web services?

> > Thanks John.  I understand now that the object itself never crosses, nor
> > are
[quoted text clipped - 5 lines]
>
> John
John Saunders - 06 Sep 2006 21:22 GMT
> How would you recommend solving the problem of moving a .net object
> between
> systems via web services?

If you have to move a .NET object between .NET systems, then you should use
.NET Remoting, not web services. .NET Remoting can be very simple to use,
and can also use SOAP over HTTP, helping remove issues with firewalls. It
can be much more powerful than web services, but it doesn't have to be - a
simple Remoting setup can be very simple and easy to maintain, and need not
require much study to use.

If you want platform independence, then you want Web Services, but Web
Services don't move objects - at best, they move XML. The XML may have come
from an object, and may happen to be destined to become an object again, but
it's safer to think of it in a totally abstract way - or to assume it will
be manipulated by a scripting language, shell script, or similar. Most shell
scripts don't know about objects, so when thinking about Web Services, don't
think about objects.

John
Jim S - 06 Sep 2006 21:29 GMT
Thanks John, I'll look into remoting.

> > How would you recommend solving the problem of moving a .net object
> > between
[quoted text clipped - 16 lines]
>
> John

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.