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 / March 2005

Tip: Looking for answers? Try searching our database.

Web Method returns array of custom type : casting error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Grandy - 23 Mar 2005 20:37 GMT
My ASP.NET Web Service project has a Web Method that returns an array filled
with instances of a custom class.

The custom class is defined in a Class Library that is included in the
web-service project.

The same class lib is included in the ASP.NET Web Application that calls the
web-method

I can successfully call the web-method with

object[] a = WebReference.WebService.WebMethod();

but when I attempt

myClass element = (myClass) a[i];

I receive the runtime error

"Specified cast is not valid."
Manohar Kamath - 23 Mar 2005 21:00 GMT
What the web method (actually the method from proxy class) is returning is a
proxy-type, and not the type that web service originally returned. You have
two choices:

1. Use the proxy type as-is. Since this was generated class, it will be
close to what the web service returned, sans any methods.

2. Manually edit the proxy class (found in webreferences folder of your web
site, with the same name as the reference page), and remove the proxy type,
and include the actual type in the cs file. Then add the corresponding using
statement as well.

Signature

Manohar Kamath
Editor, .netWire
www.dotnetwire.com

> My ASP.NET Web Service project has a Web Method that returns an array filled
> with instances of a custom class.
[quoted text clipped - 16 lines]
>
> "Specified cast is not valid."
John Grandy - 23 Mar 2005 22:14 GMT
Hi Manohar, and thanks for the response.

<<<
1. Use the proxy type as-is. Since this was generated class, it will be
close to what the web service returned, sans any methods.

My custom class is very simple.  Two private fields.  Two properties to
read-access the private fields.  Two constructors (default, and one that
sets the two fields' values).

Are you saying that the proxy type will contain the data that my custom
class contains , but the two properties will not be available ?

So, I access the data like this:

a[i].Field1
a[i].Field2

<<<
2. Manually edit the proxy class (found in webreferences folder of your web
site, with the same name as the reference page), and remove the proxy type,
and include the actual type in the cs file. Then add the corresponding using
statement as well.

I'm not sure exactly what to do here.

Looking at my web-application in Visual Studio, and clicking on the
web-reference, a number of expandable nodes are shown: these appear to be
all of the types that my web-service references.  Among them is listed the
name of my custom class -- but the first character is in lower case.  In the
tree that expands from that node are listed my class' fields, construtors,
and properties exactly as I've defined them.

I'm looking at the contents of the "Web References" folder underneath my
web-app's physical folder: Reference.cs, Reference.map,
<web-service-name>.disco, <web-service-name>.wsdl

These files all look very dangerous to edit.

> What the web method (actually the method from proxy class) is returning is
> a
[quoted text clipped - 35 lines]
>>
>> "Specified cast is not valid."
Manohar Kamath - 23 Mar 2005 23:32 GMT
I am saying, your proxy type is identical to your custom type (almost),
except it will be in another namespace -- same as that of the proxy class.
So, by doing a manual changes, you are tricking SOAP to think that it is
actually deserializing to  proxy type. As long as the names and types are
same, it will work.

Signature

Manohar Kamath
Editor, .netWire
www.dotnetwire.com

> Hi Manohar, and thanks for the response.
>
[quoted text clipped - 74 lines]
> >>
> >> "Specified cast is not valid."
John Grandy - 23 Mar 2005 23:51 GMT
Ok. I edited Reference.cs so that my custom type is explicitly declared
using the class library namespace:

used to be : public MyCustomClass Method1() {

changed to: public MyCustomClassLibrary.MyCustomClass Method1() {

Now what happens is that I receive no compile-time nor run-time errors, but
on the web-app side, properties of an instance of MyCustomClass are always
null, no matter what values MyWebService.Method1() has set them to.

so ...

object[] array = MyWebReference.Method1();
(MyCustomClass) element = array[0];
String s1 = element.Field1;
String s2 = element.Field2;

element.Field1 and element.Field2 are always null,

>I am saying, your proxy type is identical to your custom type (almost),
> except it will be in another namespace -- same as that of the proxy class.
[quoted text clipped - 88 lines]
>> >>
>> >> "Specified cast is not valid."
Manohar Kamath - 23 Mar 2005 23:57 GMT
I think you misunderstood me...

In the reference.cs, DELETE the custom type MyCustomClass. Then, add the
using statement with the namespace MyCustomClassLibrary. You should not have
to change anything else.

Signature

Manohar Kamath
Editor, .netWire
www.dotnetwire.com

> Ok. I edited Reference.cs so that my custom type is explicitly declared
> using the class library namespace:
[quoted text clipped - 108 lines]
> >> >>
> >> >> "Specified cast is not valid."
John Grandy - 24 Mar 2005 00:30 GMT
Ahh, I see. The problem is that the MyCustomClass stub in References.cs
causes the compiler to think that web-method return types of MyCustomClass
originate in the web-service rather than in MyCustomClassLibrary.

Correct ?

>I think you misunderstood me...
>
[quoted text clipped - 136 lines]
>> >> >>
>> >> >> "Specified cast is not valid."
Manohar Kamath - 24 Mar 2005 02:13 GMT
Yes... by removing the custom type, you are actually returning from the
proxy class, an object which is the same type the web service returns.

Signature

Manohar Kamath
Editor, .netWire
www.dotnetwire.com

> Ahh, I see. The problem is that the MyCustomClass stub in References.cs
> causes the compiler to think that web-method return types of MyCustomClass
[quoted text clipped - 142 lines]
> >> >> >>
> >> >> >> "Specified cast is not valid."

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.