469,085 Members | 1,048 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

InvalidCastException when casting object returned from web service

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

Sep 6 '06 #1
2 2494
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.
"Jim S" <Ji**@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
>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;
}

Sep 6 '06 #2
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

"Kevin Spencer" wrote:
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.
"Jim S" <Ji**@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
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;
}


Sep 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by CARIGAR | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.