Make a web service method return a class

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hi,

How do I make a web method to return an object class MyClass, also how do I
make it to return List<MyClass>?

TIA
 
If you mean an asmx-style web-service, then something like below - but
note that the remote proxy won't get the exact same class
representation but a lightweight data abstraction (and quite likely a
MyClass[] at the caller). With WCF it is possible to use either the
proxy approach, or via assembly-sharing you can use the exact same
class.

Marc

[Serializable]
public class MyClass {...}

[WebMethod]
public MyClass Foo(...) {...}

[WebMethod]
public List<MyClass> Bar(...) {...}
 
Danny said:
How do I make a web method to return an object class MyClass, also how do I
make it to return List<MyClass>?

Returning a MyClass is straigth forward. Make sure that MyClass
is following proper paradigms about private fields and public
properties.

You should not return a List<MyClass> from a web service but
instead a MyClass[], because List is .NET specific.

(I believe that List<MyClass> will be exposed as MyClass[], so
it will work, but be a bit confusing)

Arne
 
Back
Top