Passing objects with web services

  • Thread starter Thread starter ThatsIT.net.au
  • Start date Start date
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?
 
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
 
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.
 
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?
 
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
 
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.
 
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.
 
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
 
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.
 
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
 
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?
 
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.
 
ThatsIT.net.au said:
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

Jeff and Mr. Arnold have tried to explain that what is passed back is an
object with a class template resembling the SERVER SIDE class
PublicUser. It (the template) does not containg code behind the class.
In other words, if you make a local change such as
PublicUserObj.PhoneNumber = "703-555-1212", then if the server code
would have done some validations, they are not done on the client. The
client will not have the code to do the validations.

To get done what you want, you will need the client to have a full
definition of the class.
 
Peter K said:
ThatsIT.net.au said:
Peter K said:
"Mr. Arnold" <MR. (e-mail address removed)> skrev i en meddelelse

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.

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.

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?


Yes, i also tried with a non inherted class with the same result.

<System.Xml.Serialization.XmlIncludeAttribute(GetType(PublicUser)), _
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 MustInherit Class Person
End Class
 
Family Tree Mike said:
Jeff and Mr. Arnold have tried to explain that what is passed back is an
object with a class template resembling the SERVER SIDE class PublicUser.
It (the template) does not containg code behind the class. In other words,
if you make a local change such as PublicUserObj.PhoneNumber =
"703-555-1212", then if the server code would have done some validations,
they are not done on the client. The client will not have the code to do
the validations.

To get done what you want, you will need the client to have a full
definition of the class.


yes but how?
I think what i was trying to do is n ot posible

What I can do, is just recreat the methods of the class in the WS, and then
just pass then on to the PublicUser object

but i was wondering if the WS had the power to pass a conected object. I
guess I qwas expecting too much.


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


I don't think so without some help when dealing with objects of the type
the OP is posting about.
(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.

I don't think that it's going to generate the code by itself, without it
being told to do so based on the type of object the OP is posting about,
and even with a WCF Web Service, you have to decorate the class and
properties of the class with the [Serializable] attribute, becuase for
sure WCF expects it for an object it's sending or receiving, at the very
least [Serializable] be decorating the 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.

And may be that's OP's problem that the class he is trying to pass back is
not decorated with [Serializable] attributes, which is going to make it an
XML serialized object, which .Net will do it by itself, as I recall. I am
not sure, as I don't work with legacy ASP.NET Web services anymore only
ASP.NET WCF Web services.

He can just simple do the serialize and deserialize code himself too, but
the object in his case but be an XML serialized object.
http://www.codeproject.com/Articles/37824/Serializing-and-Deserializing-Objects-to-and-from-XML.aspx

Yeah - well, I won't try to guess any more, I'm not an expert and tend to
choose the simplest route I can. I'd actually recommend that the OP uses all
the automatic tools provided by Visual Studio he can, and return
ultra-simple objects which simply deliver data, instead of complex objects
which include all manner of methods, and what is starting to sound like
methods including some sort of remoting capabilities.

How does the custom serialisation affect the compatibility of the
web-service for example? Can my client connect to the webservice by using
the appropriate url (and wsdl)?

What does the WSDL look like, by the way?
 
Peter K said:
Mr. Arnold said:
Peter K said:
"Mr. Arnold" <MR. (e-mail address removed)> skrev i en meddelelse

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.

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


I don't think so without some help when dealing with objects of the type
the OP is posting about.
(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 don't think that it's going to generate the code by itself, without it
being told to do so based on the type of object the OP is posting about,
and even with a WCF Web Service, you have to decorate the class and
properties of the class with the [Serializable] attribute, becuase for
sure WCF expects it for an object it's sending or receiving, at the very
least [Serializable] be decorating the 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.

And may be that's OP's problem that the class he is trying to pass back
is not decorated with [Serializable] attributes, which is going to make
it an XML serialized object, which .Net will do it by itself, as I
recall. I am not sure, as I don't work with legacy ASP.NET Web services
anymore only ASP.NET WCF Web services.

He can just simple do the serialize and deserialize code himself too, but
the object in his case but be an XML serialized object.
http://www.codeproject.com/Articles/37824/Serializing-and-Deserializing-Objects-to-and-from-XML.aspx

Yeah - well, I won't try to guess any more, I'm not an expert and tend to
choose the simplest route I can. I'd actually recommend that the OP uses
all the automatic tools provided by Visual Studio he can, and return
ultra-simple objects which simply deliver data, instead of complex objects
which include all manner of methods, and what is starting to sound like
methods including some sort of remoting capabilities.

Yes that is what i was trying to do, not so much that i need it, but to see
just what i could do with a WS

I have decided against, but i will want to pass some simple objects
(properties only) all the same as you metion above.
I will want to pass them back also
i'll give it a go in the morning.

Thank all
 
Peter K said:
Mr. Arnold said:
Peter K said:
"Mr. Arnold" <MR. (e-mail address removed)> skrev i en meddelelse

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.

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


I don't think so without some help when dealing with objects of the type
the OP is posting about.
(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 don't think that it's going to generate the code by itself, without it
being told to do so based on the type of object the OP is posting about,
and even with a WCF Web Service, you have to decorate the class and
properties of the class with the [Serializable] attribute, becuase for
sure WCF expects it for an object it's sending or receiving, at the very
least [Serializable] be decorating the 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.

And may be that's OP's problem that the class he is trying to pass back
is not decorated with [Serializable] attributes, which is going to make
it an XML serialized object, which .Net will do it by itself, as I
recall. I am not sure, as I don't work with legacy ASP.NET Web services
anymore only ASP.NET WCF Web services.

He can just simple do the serialize and deserialize code himself too, but
the object in his case but be an XML serialized object.
http://www.codeproject.com/Articles/37824/Serializing-and-Deserializing-Objects-to-and-from-XML.aspx

Yeah - well, I won't try to guess any more, I'm not an expert and tend to
choose the simplest route I can. I'd actually recommend that the OP uses
all the automatic tools provided by Visual Studio he can, and return
ultra-simple objects which simply deliver data, instead of complex objects
which include all manner of methods, and what is starting to sound like
methods including some sort of remoting capabilities.

Yes that is what i was trying to do, not so much that i need it, but to see
just what i could do with a WS

I have decided against, but i will want to pass some simple objects
(properties only) all the same as you metion above.
I will want to pass them back also
i'll give it a go in the morning.

Thank all
 
ThatsIT.net.au said:
yes but how?
I think what i was trying to do is n ot posible

What I can do, is just recreat the methods of the class in the WS, and
then just pass then on to the PublicUser object

but i was wondering if the WS had the power to pass a conected object.
I guess I qwas expecting too much.


Thanks

A way of doing this is to have three divisions of code 1) Server 2)
Client 3) Shared. The PublicUser class sounds like it would be put into
the Shared section of code. Think of this as three dlls. The client
has ClientLibrary.dll, and SharedLibrary.dll while the server has
ServerLibrary.dll and SharedLibrary.dll.

You then need to add code in your ClientLibrary that after the server
object is fetched, the properties returned are used to fill out a
SharedLibrary object.

Another approach is to open methods on the WS that execute the methods
on an object sent back, as you suggested. There are trade offs with
each method. Frequency of server hits, data consistency if this server
app is a data management system and data transfer times, to name a few.
 
Back
Top