Groups | Blog | Home
all groups > dotnet web services > september 2006 >

dotnet web services : Passing class with methods to a WS. Data not methods serialized



twahl
9/22/2006 11:52:01 AM
Hi,
In the WS I've defined a class that contains both data and methods. I'm
trying to pass this class to a client application using a web method. When I
access this class on the client only the properties are available not the
methods. I guess this makes sense but how can I get access/instantiate to
the complete class containing both the data and the methods?
Thanks,
Terry
twahl
9/22/2006 2:34:01 PM
Hi John,
Thanks again for the help! Your explanation helped a lot.

If I have a web method returning an object that I wish to deserialize it to
an instance of a class? If possible can you provide an example in code as to
how I would do that or possibly point me to location that I can read up on it?

Thanks again,
Terry


[quoted text, click to view]
John Saunders
9/22/2006 4:53:17 PM
[quoted text, click to view]

You can't. Web Services don't work that way.

What happens is that there is a class on the server. For the sake of
discussion, let's call it "Employee". Emplyee has properties like ID and
Name and DateOfBirth. It has methods like Hire and Fire and Pay.

The client calls an operation on your web service, and you want to send an
Employee back. Your web service acquires an instance of the Employee class.
This instance gets serialized into the response to the web service
operation.

All that gets serialized is the data. Only XML, which conforms to the schema
which is a part of the WSDL file describing your web service.

The client receives this XML. Most clients will deserialize this into an
instance of a class, which may happen to be called "Employee". That need not
be the case. The client platform could choose to use a class called
WEBSERVICEMPLOYEE if it likes. It's also useful to keep in mind that not all
clients are object oriented!

All the client was sent was XML. The most the client can receive is data
which is deserialized from this XML. No methods were transmitted, so none
were received!

If you need your .NET Client and .NET Server to use the actual, identical
class, then you need to use .NET Remoting, not Web Services.

John

John Saunders
9/22/2006 8:56:05 PM
[quoted text, click to view]

public class Employee
{
protected DateTime _fireDate;

public int ID {get {return _id;}}
public string Name {get {return _name;}}
public DateTime BirthDate {get {return _date;}}
public DateTime TerminationDate {get {return _fireDate;}}
}

public class EmployeeWithMethods : Employee
{
public void Fire() {_fireDate = DateTime.Now;}
}


John

twahl
9/26/2006 11:15:02 AM
Hi John,
How would I deserialize this class in a web method? This is my biggest area
of confusion. Again thanks for your help!
Terry


[quoted text, click to view]
John Saunders
9/26/2006 5:46:05 PM
[quoted text, click to view]

Yeah, that's a good point. Sometimes I revert to my roots in C/C++, where
such things are only a cast away.

There is probably something that can be done using attribtues on the method
parameters, to tell .NET that when it sees a certain XML Element, it should
deserialize it to a particular class. But I don't know which attribute this
would be (maybe XmlElementAttribute or XmlTypeAttribute).

If that doesn't help, I'd personally punt and use delegation. I'd have my
EmployeeWithMethods class contain an instance of the deserialized class, and
just delegate all of the properties to this instance.

John

AddThis Social Bookmark Button