InvalidCastException when casting object returned from web service

G

Guest

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.
// ObjectA objectA = ws.GetObjectAForPocketPC (someParamA, someParamB)
return objectA;
}
 
K

Kevin Spencer

The returned object is of the type that it was when the Web Service
generated it, not an identical but different class which you copied.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
 
G

Guest

ok, how should I cast it to the client.ObjectA? Do I need to enumerate
through webservice.ObjectA's types and copy them each to client.ObjectA? I
hope not.
Thanks,
Jim
 

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

Top