Make a web service method return a class

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
 
M

Marc Gravell

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(...) {...}
 
A

Arne Vajhøj

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
 

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

Top