Connecting Tech Pros Worldwide Forums | Help | Site Map

Web Method returns array of custom type : casting error

John Grandy
Guest
 
Posts: n/a
#1: Nov 23 '05
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
Guest
 
Posts: n/a
#2: Nov 23 '05

re: Web Method returns array of custom type : casting error


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.


--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...[color=blue]
> My ASP.NET Web Service project has a Web Method that returns an array[/color]
filled[color=blue]
> 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[/color]
the[color=blue]
> 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."
>
>
>
>
>
>
>[/color]


John Grandy
Guest
 
Posts: n/a
#3: Nov 23 '05

re: Web Method returns array of custom type : casting error


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.[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

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.[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

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.


"Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
news:uAtjyL%23LFHA.3632@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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.
>
>
> --
> Manohar Kamath
> Editor, .netWire
> www.dotnetwire.com
>
>
> "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...[color=green]
>> My ASP.NET Web Service project has a Web Method that returns an array[/color]
> filled[color=green]
>> 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[/color]
> the[color=green]
>> 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."
>>
>>
>>
>>
>>
>>
>>[/color]
>
>[/color]


Manohar Kamath
Guest
 
Posts: n/a
#4: Nov 23 '05

re: Web Method returns array of custom type : casting error


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.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:u%23DoV1%23LFHA.1300@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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.[color=green][color=darkred]
> >>>[/color][/color]
>
> 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[/color]
web[color=blue]
> site, with the same name as the reference page), and remove the proxy[/color]
type,[color=blue]
> and include the actual type in the cs file. Then add the corresponding[/color]
using[color=blue]
> statement as well.[color=green][color=darkred]
> >>>[/color][/color]
>
> 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[/color]
the[color=blue]
> 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.
>
>
> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
> news:uAtjyL%23LFHA.3632@TK2MSFTNGP10.phx.gbl...[color=green]
> > What the web method (actually the method from proxy class) is returning[/color][/color]
is[color=blue][color=green]
> > 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.
> >
> >
> > --
> > Manohar Kamath
> > Editor, .netWire
> > www.dotnetwire.com
> >
> >
> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> > news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...[color=darkred]
> >> My ASP.NET Web Service project has a Web Method that returns an array[/color]
> > filled[color=darkred]
> >> 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[/color][/color][/color]
calls[color=blue][color=green]
> > the[color=darkred]
> >> 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."
> >>
> >>
> >>
> >>
> >>
> >>
> >>[/color]
> >
> >[/color]
>
>[/color]


John Grandy
Guest
 
Posts: n/a
#5: Nov 23 '05

re: Web Method returns array of custom type : casting error


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,



"Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
news:eRZ1zg$LFHA.2420@TK2MSFTNGP12.phx.gbl...[color=blue]
>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.
>
> --
> Manohar Kamath
> Editor, .netWire
> www.dotnetwire.com
>
>
> "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> news:u%23DoV1%23LFHA.1300@TK2MSFTNGP10.phx.gbl...[color=green]
>> 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.[color=darkred]
>> >>>[/color]
>>
>> 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[/color]
> web[color=green]
>> site, with the same name as the reference page), and remove the proxy[/color]
> type,[color=green]
>> and include the actual type in the cs file. Then add the corresponding[/color]
> using[color=green]
>> statement as well.[color=darkred]
>> >>>[/color]
>>
>> 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[/color]
> the[color=green]
>> 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.
>>
>>
>> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
>> news:uAtjyL%23LFHA.3632@TK2MSFTNGP10.phx.gbl...[color=darkred]
>> > What the web method (actually the method from proxy class) is returning[/color][/color]
> is[color=green][color=darkred]
>> > 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.
>> >
>> >
>> > --
>> > Manohar Kamath
>> > Editor, .netWire
>> > www.dotnetwire.com
>> >
>> >
>> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
>> > news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...
>> >> 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[/color][/color]
> calls[color=green][color=darkred]
>> > 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."
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Manohar Kamath
Guest
 
Posts: n/a
#6: Nov 23 '05

re: Web Method returns array of custom type : casting error


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.


--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:e1s6Tr$LFHA.1172@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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,[/color]
but[color=blue]
> 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,
>
>
>
> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
> news:eRZ1zg$LFHA.2420@TK2MSFTNGP12.phx.gbl...[color=green]
> >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[/color][/color]
class.[color=blue][color=green]
> > 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[/color][/color]
are[color=blue][color=green]
> > same, it will work.
> >
> > --
> > Manohar Kamath
> > Editor, .netWire
> > www.dotnetwire.com
> >
> >
> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> > news:u%23DoV1%23LFHA.1300@TK2MSFTNGP10.phx.gbl...[color=darkred]
> >> 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[/color][/color][/color]
that[color=blue][color=green][color=darkred]
> >> 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[/color]
> > web[color=darkred]
> >> site, with the same name as the reference page), and remove the proxy[/color]
> > type,[color=darkred]
> >> and include the actual type in the cs file. Then add the corresponding[/color]
> > using[color=darkred]
> >> 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[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> >> 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.[/color][/color][/color]
In[color=blue][color=green]
> > the[color=darkred]
> >> 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[/color][/color][/color]
my[color=blue][color=green][color=darkred]
> >> 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.
> >>
> >>
> >> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
> >> news:uAtjyL%23LFHA.3632@TK2MSFTNGP10.phx.gbl...
> >> > What the web method (actually the method from proxy class) is[/color][/color][/color]
returning[color=blue][color=green]
> > is[color=darkred]
> >> > a
> >> > proxy-type, and not the type that web service originally returned.[/color][/color][/color]
You[color=blue][color=green][color=darkred]
> >> > have
> >> > two choices:
> >> >
> >> > 1. Use the proxy type as-is. Since this was generated class, it will[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> >> > close to what the web service returned, sans any methods.
> >> >
> >> > 2. Manually edit the proxy class (found in webreferences folder of[/color][/color][/color]
your[color=blue][color=green][color=darkred]
> >> > 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[/color][/color][/color]
corresponding[color=blue][color=green][color=darkred]
> >> > using
> >> > statement as well.
> >> >
> >> >
> >> > --
> >> > Manohar Kamath
> >> > Editor, .netWire
> >> > www.dotnetwire.com
> >> >
> >> >
> >> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> >> > news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...
> >> >> My ASP.NET Web Service project has a Web Method that returns an[/color][/color][/color]
array[color=blue][color=green][color=darkred]
> >> > filled
> >> >> with instances of a custom class.
> >> >>
> >> >> The custom class is defined in a Class Library that is included in[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >> >> web-service project.
> >> >>
> >> >> The same class lib is included in the ASP.NET Web Application that[/color]
> > calls[color=darkred]
> >> > 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."
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>[/color]


John Grandy
Guest
 
Posts: n/a
#7: Nov 23 '05

re: Web Method returns array of custom type : casting error


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 ?

"Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
news:emuTzu$LFHA.1396@TK2MSFTNGP10.phx.gbl...[color=blue]
>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.
>
>
> --
> Manohar Kamath
> Editor, .netWire
> www.dotnetwire.com
>
>
> "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> news:e1s6Tr$LFHA.1172@TK2MSFTNGP12.phx.gbl...[color=green]
>> 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,[/color]
> but[color=green]
>> 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,
>>
>>
>>
>> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
>> news:eRZ1zg$LFHA.2420@TK2MSFTNGP12.phx.gbl...[color=darkred]
>> >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[/color][/color]
> class.[color=green][color=darkred]
>> > 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[/color][/color]
> are[color=green][color=darkred]
>> > same, it will work.
>> >
>> > --
>> > Manohar Kamath
>> > Editor, .netWire
>> > www.dotnetwire.com
>> >
>> >
>> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
>> > news:u%23DoV1%23LFHA.1300@TK2MSFTNGP10.phx.gbl...
>> >> 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[/color][/color]
> that[color=green][color=darkred]
>> >> 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[/color][/color]
> be[color=green][color=darkred]
>> >> 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.[/color][/color]
> In[color=green][color=darkred]
>> > 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[/color][/color]
> my[color=green][color=darkred]
>> >> 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.
>> >>
>> >>
>> >> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
>> >> news:uAtjyL%23LFHA.3632@TK2MSFTNGP10.phx.gbl...
>> >> > What the web method (actually the method from proxy class) is[/color][/color]
> returning[color=green][color=darkred]
>> > is
>> >> > a
>> >> > proxy-type, and not the type that web service originally returned.[/color][/color]
> You[color=green][color=darkred]
>> >> > have
>> >> > two choices:
>> >> >
>> >> > 1. Use the proxy type as-is. Since this was generated class, it will[/color][/color]
> be[color=green][color=darkred]
>> >> > close to what the web service returned, sans any methods.
>> >> >
>> >> > 2. Manually edit the proxy class (found in webreferences folder of[/color][/color]
> your[color=green][color=darkred]
>> >> > 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[/color][/color]
> corresponding[color=green][color=darkred]
>> >> > using
>> >> > statement as well.
>> >> >
>> >> >
>> >> > --
>> >> > Manohar Kamath
>> >> > Editor, .netWire
>> >> > www.dotnetwire.com
>> >> >
>> >> >
>> >> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
>> >> > news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...
>> >> >> My ASP.NET Web Service project has a Web Method that returns an[/color][/color]
> array[color=green][color=darkred]
>> >> > filled
>> >> >> with instances of a custom class.
>> >> >>
>> >> >> The custom class is defined in a Class Library that is included in[/color][/color]
> the[color=green][color=darkred]
>> >> >> 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."
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Manohar Kamath
Guest
 
Posts: n/a
#8: Nov 23 '05

re: Web Method returns array of custom type : casting error


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.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:uRFH9AAMFHA.3500@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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 ?
>
> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
> news:emuTzu$LFHA.1396@TK2MSFTNGP10.phx.gbl...[color=green]
> >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.
> >
> >
> > --
> > Manohar Kamath
> > Editor, .netWire
> > www.dotnetwire.com
> >
> >
> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> > news:e1s6Tr$LFHA.1172@TK2MSFTNGP12.phx.gbl...[color=darkred]
> >> 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,[/color]
> > but[color=darkred]
> >> 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,
> >>
> >>
> >>
> >> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
> >> news:eRZ1zg$LFHA.2420@TK2MSFTNGP12.phx.gbl...
> >> >I am saying, your proxy type is identical to your custom type[/color][/color][/color]
(almost),[color=blue][color=green][color=darkred]
> >> > except it will be in another namespace -- same as that of the proxy[/color]
> > class.[color=darkred]
> >> > 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[/color]
> > are[color=darkred]
> >> > same, it will work.
> >> >
> >> > --
> >> > Manohar Kamath
> >> > Editor, .netWire
> >> > www.dotnetwire.com
> >> >
> >> >
> >> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> >> > news:u%23DoV1%23LFHA.1300@TK2MSFTNGP10.phx.gbl...
> >> >> 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[/color]
> > that[color=darkred]
> >> >> 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[/color][/color][/color]
proxy[color=blue][color=green][color=darkred]
> >> > type,
> >> >> and include the actual type in the cs file. Then add the[/color][/color][/color]
corresponding[color=blue][color=green][color=darkred]
> >> > 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[/color][/color][/color]
to[color=blue][color=green]
> > be[color=darkred]
> >> >> all of the types that my web-service references. Among them is[/color][/color][/color]
listed[color=blue][color=green][color=darkred]
> >> >> the
> >> >> name of my custom class -- but the first character is in lower case.[/color]
> > In[color=darkred]
> >> > 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[/color][/color][/color]
underneath[color=blue][color=green]
> > my[color=darkred]
> >> >> 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.
> >> >>
> >> >>
> >> >> "Manohar Kamath" <mkamath@TAKETHISOUTkamath.com> wrote in message
> >> >> news:uAtjyL%23LFHA.3632@TK2MSFTNGP10.phx.gbl...
> >> >> > What the web method (actually the method from proxy class) is[/color]
> > returning[color=darkred]
> >> > is
> >> >> > a
> >> >> > proxy-type, and not the type that web service originally returned.[/color]
> > You[color=darkred]
> >> >> > have
> >> >> > two choices:
> >> >> >
> >> >> > 1. Use the proxy type as-is. Since this was generated class, it[/color][/color][/color]
will[color=blue][color=green]
> > be[color=darkred]
> >> >> > close to what the web service returned, sans any methods.
> >> >> >
> >> >> > 2. Manually edit the proxy class (found in webreferences folder of[/color]
> > your[color=darkred]
> >> >> > 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[/color]
> > corresponding[color=darkred]
> >> >> > using
> >> >> > statement as well.
> >> >> >
> >> >> >
> >> >> > --
> >> >> > Manohar Kamath
> >> >> > Editor, .netWire
> >> >> > www.dotnetwire.com
> >> >> >
> >> >> >
> >> >> > "John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
> >> >> > news:%23Mke5%239LFHA.3928@TK2MSFTNGP09.phx.gbl...
> >> >> >> My ASP.NET Web Service project has a Web Method that returns an[/color]
> > array[color=darkred]
> >> >> > filled
> >> >> >> with instances of a custom class.
> >> >> >>
> >> >> >> The custom class is defined in a Class Library that is included[/color][/color][/color]
in[color=blue][color=green]
> > the[color=darkred]
> >> >> >> web-service project.
> >> >> >>
> >> >> >> The same class lib is included in the ASP.NET Web Application[/color][/color][/color]
that[color=blue][color=green][color=darkred]
> >> > 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."
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>[/color]


Closed Thread


Similar .NET Framework bytes