Passing objects with web services

T

ThatsIT.net.au

I have a object that I want to pass back though a web service to the
consumer. when I do this i get the object but can not see any methods. the
object is decleared in the webservice as PublicUser, in the consuming web
site PublicUser comes up in intellisence so i know that it must be comming
though ok, but I can not access its methods.

Am i missing something any ideas?
 
P

Peter K

ThatsIT.net.au said:
I have a object that I want to pass back though a web service to the
consumer. when I do this i get the object but can not see any methods. the
object is decleared in the webservice as PublicUser, in the consuming web
site PublicUser comes up in intellisence so i know that it must be comming
though ok, but I can not access its methods.

Am i missing something any ideas?

Hard to say what your problem could be. Are the properties in your custom
class public?

This is a nice little intro to passing custom objects from a webservice:
http://www.dalepreston.com/Blog/2005/02/returning-custom-classes-from-web.html


/Peter
 
P

Peter K

Mark Rae said:
Are you casting the deserialised object back to a PublicUser...?

The times I've written consumers of webservices, this has not been
necessary. I use Visual Studio to create the appropriate proxy, and the
generated class can simply be used by my consumer.

For example, in Visual Studio, I create a "web reference" to the webservice
at http://somewebservice.asmx,

Then in code I can do something like:

SomeWebServiceRef.WebServiceName ws = new
SomeWebServiceRef.WebServiceName();
SomeWebServiceRef.MyCustomClass result = ws.GetTheResult(123);


/Peter
 
P

Peter K

Mark Rae said:
Indulge me...


Before running those three lines, what does
was.GetTheResult(123).GetType(); reveal...?

One can see that the type is for example "SomeWebServiceRef.MyCustomClass".
This type is generated by Visual Studio as part of the proxy-generation
process - and the class can be found in the generated "Reference.cs".

I'm not quite sure where your line of questioning is headed (and I know you
are aware of all this and a whole lot more). I simply use the tools provided
by Visual Studio, and I don't have to worry about casting any results from
calls to web-methods - all the deserialisation/casting is handled by the
code generated by Visual Studio.

If the OP uses Visual Studio to make a web-reference to his web-service then
he'll be able to use a class called something like
webRef.PublicUser. Perhaps if the OP posted a real small example showing his
web-service code, and the client code, then it would be easier to find out
what the problem could be.
 
J

Jeff Johnson

I have a object that I want to pass back though a web service to the
consumer. when I do this i get the object but can not see any methods. the
object is decleared in the webservice as PublicUser, in the consuming web
site PublicUser comes up in intellisence so i know that it must be comming
though ok, but I can not access its methods.

Am i missing something any ideas?

[Apologies if I'm telling you something you already know.]

Web services create proxy classes to pass objects. These proxy classes only
have properties, no methods. You have to convert your custom class to a
proxy class before sending and, in the other direction, you have to convert
the received proxy object to an instance of your actual custom class. Only
when dealing with an instance of the original class will you see the methods
that class provides. This not only means that the afore-mentioned conversion
is necessary, but also that the client needs the custom class definition
(i.e., the client must have some assembly with that class definition in it).

Is it possible you're dealing with a proxy class and not the original? If
so, that's why you don't see methods.
 
T

ThatsIT.net.au

Mark Rae said:
Are you casting the deserialised object back to a PublicUser...?


yes i tried that

when you say deserialised am I suposed to deserialise it manulay, i have
tried to serlizing the webservice made no difference, I tried to serialise
the method but it errored
 
T

ThatsIT.net.au

Peter K said:
The times I've written consumers of webservices, this has not been
necessary. I use Visual Studio to create the appropriate proxy, and the
generated class can simply be used by my consumer.

For example, in Visual Studio, I create a "web reference" to the
webservice at http://somewebservice.asmx,

Then in code I can do something like:

SomeWebServiceRef.WebServiceName ws = new
SomeWebServiceRef.WebServiceName();
SomeWebServiceRef.MyCustomClass result = ws.GetTheResult(123);


/Peter

that looks the same


Dim w As WebUser = New WebUser
Dim oUser As PublicUser = CType(w.getUser, PublicUser)
Response.Write(oUser.www)

www is a method, but it gives an error saying www is not a method of oUser,
by the way I can access www in the web service, so object is correct at that
point.


webService below




<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://thatsbooking.com.au/WebUser")>
_
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
_
<Serializable()> _
<ToolboxItem(False)> _
Public Class WebUser
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
Public Function getUser() As TBBO.PublicUser
Dim u As TBBO.PublicUser = New TBBO.PublicUser
Return u
End Function

Public Sub New()

End Sub
End Class
 
T

ThatsIT.net.au

Peter K said:
One can see that the type is for example
"SomeWebServiceRef.MyCustomClass". This type is generated by Visual Studio
as part of the proxy-generation process - and the class can be found in
the generated "Reference.cs".

I'm not quite sure where your line of questioning is headed (and I know
you are aware of all this and a whole lot more). I simply use the tools
provided by Visual Studio, and I don't have to worry about casting any
results from calls to web-methods - all the deserialisation/casting is
handled by the code generated by Visual Studio.

If the OP uses Visual Studio to make a web-reference to his web-service
then he'll be able to use a class called something like
webRef.PublicUser. Perhaps if the OP posted a real small example showing
his web-service code, and the client code, then it would be easier to find
out what the problem could be.


just posted them in previous post.

I am using visual studio 2008 to create reference and to create web service.
 
T

ThatsIT.net.au

Jeff Johnson said:
I have a object that I want to pass back though a web service to the
consumer. when I do this i get the object but can not see any methods. the
object is decleared in the webservice as PublicUser, in the consuming web
site PublicUser comes up in intellisence so i know that it must be comming
though ok, but I can not access its methods.

Am i missing something any ideas?

[Apologies if I'm telling you something you already know.]

Web services create proxy classes to pass objects. These proxy classes
only have properties, no methods. You have to convert your custom class to
a proxy class before sending and, in the other direction, you have to
convert the received proxy object to an instance of your actual custom
class. Only when dealing with an instance of the original class will you
see the methods that class provides. This not only means that the
afore-mentioned conversion is necessary, but also that the client needs
the custom class definition (i.e., the client must have some assembly with
that class definition in it).

Is it possible you're dealing with a proxy class and not the original? If
so, that's why you don't see methods.


I have not converted to a proxy class as far as I know, unless this is done
by visual studio.
How do I find out?
 
M

Mr. Arnold

ThatsIT.net.au said:
I have a object that I want to pass back though a web service to the
consumer. when I do this i get the object but can not see any methods. the
object is decleared in the webservice as PublicUser, in the consuming web
site PublicUser comes up in intellisence so i know that it must be comming
though ok, but I can not access its methods.

When you pass an object like that back from the Web service, the methods are
left behind, and you only have access to public properties of the object on
the client side.

You have to serialize the object on the Web service side and pass back an
XML serialized object to the client.

You have to deserialize the XML serialize object and cast it back to
PublicUser on the client side. You will then have the data in the
PublicUser, and you can then access any public properties and methods of the
object at that time, on the client side.

PublicUser.vb must be a file in the Web service project, and it must be a
file in the client project, so that you can serialize, deserialize and cast
back to the object.

The Web service method is going to return 'string', since a XML serialized
object is 'string' when it's serialized.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4353 (20090820) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
P

Peter K

ThatsIT.net.au said:
that looks the same

Dim w As WebUser = New WebUser
Dim oUser As PublicUser = CType(w.getUser, PublicUser)
Response.Write(oUser.www)

www is a method, but it gives an error saying www is not a method of
oUser, by the way I can access www in the web service, so object is
correct at that point.

When you say "method" do you really mean method, or do you mean "property".
As far as I am aware, you cannot pass a class'es methods via a webservice -
only the propeties (ie getters and setters) will be passed. Usually I only
use simple "data" objects (I think they can also be termed "DTO"s) which
only have data - no functionality as such.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://thatsbooking.com.au/WebUser")>
_
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
_
<Serializable()> _
<ToolboxItem(False)> _
Public Class WebUser
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
Public Function getUser() As TBBO.PublicUser
Dim u As TBBO.PublicUser = New TBBO.PublicUser
Return u
End Function

Public Sub New()

End Sub
End Class


What about your PublicUser class, and your client. Sometimes it can be
helpful to quickly create a little ConsoleApp to act as a test client for
your webservice.
 
P

Peter K

ThatsIT.net.au said:
I have not converted to a proxy class as far as I know, unless this is
done by visual studio.

If, in your consumer/client, you have used Visual Studio to create a "web
reference" to your web-service, then yes. Visual Studio will have created a
proxy for your client - and will perform the necessary deserialisation and
casting so the client can simply use PublicUser. This is not the same
"PublicUser" as your web-service uses, it is a new class (with the same
name) that your client uses.

I normally do it like that - because it is so easy.

You can find the visual studio generated code somewhere in your client
solution. With c# it will be called Reference.cs, and here you can find a
class called PublicUser.

I usually try to keep web-services and any clients separated in their own
solutions so I don't get confused about which class really belongs where.
 
T

ThatsIT.net.au

Mr. Arnold said:
When you pass an object like that back from the Web service, the methods
are left behind, and you only have access to public properties of the
object on the client side.

You have to serialize the object on the Web service side and pass back an
XML serialized object to the client.

do you mean to mark it serlizable
or somthing like this

Private Function Serialize(ByVal obj As Object) As Byte()
Dim binaryFormatter As BinaryFormatter = New BinaryFormatter()
Dim memoryStream As MemoryStream = New MemoryStream()
binaryFormatter.Serialize(memoryStream, obj)
Return memoryStream.ToArray()
End Function

You have to deserialize the XML serialize object and cast it back to
PublicUser on the client side. You will then have the data in the
PublicUser, and you can then access any public properties and methods of
the object at that time, on the client side.


so the PublicUser classi get from the WS for casting the XML back to object,
that explains a lot, I could not understand why i got a PublicUser back at
all.



PublicUser.vb must be a file in the Web service project, and it must be a
file in the client project, so that you can serialize, deserialize and
cast back to the object.

Yes i do
PublicUser.vb
PublicUser.asmx
The Web service method is going to return 'string', since a XML serialized
object is 'string' when it's serialized.

then i have the wrong Idea on how to serlize it.
the code i have above returns byte()

do you have a example?

Thanks for your help.

dont go too far I have a few questions on WS authentication later.
 
T

ThatsIT.net.au

Peter K said:
If, in your consumer/client, you have used Visual Studio to create a "web
reference" to your web-service, then yes. Visual Studio will have created
a proxy for your client - and will perform the necessary deserialisation
and casting so the client can simply use PublicUser. This is not the same
"PublicUser" as your web-service uses, it is a new class (with the same
name) that your client uses.

I normally do it like that - because it is so easy.

You can find the visual studio generated code somewhere in your client
solution. With c# it will be called Reference.cs, and here you can find a
class called PublicUser.

I usually try to keep web-services and any clients separated in their own
solutions so I don't get confused about which class really belongs where.


That's how I have done it, I'm using vb, but yes I Have the Reference.vb
file and I can see PublicUser in it, I also have a PublicUser.data file

But I still don't have the methods or properties
 
T

ThatsIT.net.au

Peter K said:
When you say "method" do you really mean method, or do you mean
"property". As far as I am aware, you cannot pass a class'es methods via a
webservice - only the propeties (ie getters and setters) will be passed.
Usually I only use simple "data" objects (I think they can also be termed
"DTO"s) which only have data - no functionality as such.

Yes I do Mean methods, but I have tried with properties also.
 
P

Peter K

Mr. Arnold said:
When you pass an object like that back from the Web service, the methods
are left behind, and you only have access to public properties of the
object on the client side.

You have to serialize the object on the Web service side and pass back an
XML serialized object to the client.

But aren't the details of this normally handled by code automatically
generated by Visual Studio for example?

(I've only had to manually serialise some data in a WCF service we were
developing for .net <-> .net applications).

At least as far as simple web-services go, I've let VS handle all the hard
stuff for me.

You have to deserialize the XML serialize object and cast it back to
PublicUser on the client side. You will then have the data in the
PublicUser, and you can then access any public properties and methods of
the object at that time, on the client side.

Again, it appears to me that Visual Studio automatically generates the code
that handles this. Doesn't it? I'd be the first to admit I could be
completely wrong, and have just been incredibly lucky with the few simple
web-services I've written over the last few years - but for me, at least,
I've let Visual Studio do all this, and I simply call the generated proxy
and use the "PublicUser" object.

I also always thought one couldn't actually send "methods" of a custom
object via a webservice. I thought it was only possible to send "properties"
(or data). I've really only had success with that anyway. Guess I'll do a
little more digging myself.
 
T

ThatsIT.net.au

Peter K said:
But aren't the details of this normally handled by code automatically
generated by Visual Studio for example?

(I've only had to manually serialise some data in a WCF service we were
developing for .net <-> .net applications).

At least as far as simple web-services go, I've let VS handle all the hard
stuff for me.



Again, it appears to me that Visual Studio automatically generates the
code that handles this. Doesn't it? I'd be the first to admit I could be
completely wrong, and have just been incredibly lucky with the few simple
web-services I've written over the last few years - but for me, at least,
I've let Visual Studio do all this, and I simply call the generated proxy
and use the "PublicUser" object.

it looks that way to me in the code in Reference.vb

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.4016"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://blah
blah/WebUser")> _
Partial Public Class PublicUser
Inherits Person
End Class
 
P

Peter K

ThatsIT.net.au said:
Peter K said:
But aren't the details of this normally handled by code automatically
generated by Visual Studio for example?

(I've only had to manually serialise some data in a WCF service we were
developing for .net <-> .net applications).

At least as far as simple web-services go, I've let VS handle all the
hard stuff for me.



Again, it appears to me that Visual Studio automatically generates the
code that handles this. Doesn't it? I'd be the first to admit I could be
completely wrong, and have just been incredibly lucky with the few simple
web-services I've written over the last few years - but for me, at least,
I've let Visual Studio do all this, and I simply call the generated proxy
and use the "PublicUser" object.

it looks that way to me in the code in Reference.vb

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.4016"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://blah
blah/WebUser")> _
Partial Public Class PublicUser
Inherits Person
End Class

Well it is obvious the PublicUser class has no properties or methods - and
it appears it descends from a class called Person. You need to ensure that
the Person class is also serialised so your client knows about it. Is there
a Person class in the Reference.vb code?
 
T

ThatsIT.net.au

If I should get this object to work, what can this object do, do I have to
pass it back to the web service to get changes or is it somehow connected?
will I be able to some how contact database thought object?

Thanks


ThatsIT.net.au said:
Peter K said:
But aren't the details of this normally handled by code automatically
generated by Visual Studio for example?

(I've only had to manually serialise some data in a WCF service we were
developing for .net <-> .net applications).

At least as far as simple web-services go, I've let VS handle all the
hard stuff for me.



Again, it appears to me that Visual Studio automatically generates the
code that handles this. Doesn't it? I'd be the first to admit I could be
completely wrong, and have just been incredibly lucky with the few simple
web-services I've written over the last few years - but for me, at least,
I've let Visual Studio do all this, and I simply call the generated proxy
and use the "PublicUser" object.

it looks that way to me in the code in Reference.vb

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.4016"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://blah
blah/WebUser")> _
Partial Public Class PublicUser
Inherits Person
End Class

I also always thought one couldn't actually send "methods" of a custom
object via a webservice. I thought it was only possible to send
"properties" (or data). I've really only had success with that anyway.
Guess I'll do a little more digging myself.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top