The following question has been asked many times "How can I return a list of custom objects from my web service?"
In this post I will explain different methods for sending the list of domain objects back to the client.
If you are using .NET 2.0 framework then you can simply return a generic list back to the client.
You can have method like the following:
[WebMethod]
public List<Customer> GetCustomerList()
{
List<Customer> list = new List<Customer>();
list.Add(new Customer("Mohammad", "Azam"));
list.Add(new Customer("John", "Doe"));
return list;
}
Then you will add the web reference to your client project and access this web method. One interesting thing that you must note is that since you are returning a list of Customer objects the Customer object is serialized and placed in the WSDL file and also in the Reference.cs file in your client project.

Now, on the client side you will access this web service using the following code:
FooClient.FooService service = new WebApps.FooClient.FooService();
List<FooClient.Customer> list = new
List<WebApps.FooClient.Customer>(service.GetCustomerList());
Response.Write(list[0].FirstName);
Now, let's take a step back to the good old days of ArrayList. Our web service method will look like the following:
[WebMethod]
public ArrayList GetCustomerList()
{
ArrayList list = new ArrayList();
list.Add(new Customer("Mohammad", "Azam"));
return list;
}
Rebuild your application and update the web references. Now, when you are instantiating the FooClient proxy you will realize that the Customer class is gone from the proxy implementation.

Yikes! what happened?
You need to tell the web service that the type of objects that the ArrayList will contain will be of type Customer. You can do something like this:
[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(Customer))]
public ArrayList GetCustomerList()
{
ArrayList list = new ArrayList();
list.Add(new Customer("Mohammad", "Azam"));
return list;
}
The attribute tells the web service that the ArrayList will contain the elements of type Customer.
Rebuild the application and update the web references. Now, you will be able to access the Customer class from the client code.
FooClient.Customer customer = new WebApps.FooClient.Customer();
customer.FirstName = "Mohammad";
customer.LastName = "Azam";
Another question which is quite frequently asked is that how can I access the Customer object if my web service does not have a Customer object return type or Customer object input parameter. This means that the web service is taking plain types like Int32, Double, String etc.
Here is the implementation of such a web service:
[WebMethod]
public void SaveCustomer(string firstName, string lastName)
{
// do something!
}
Now, you won't have access to the Customer object from the client side. Here is one way to expose a WebMethod whose main purpose is to expose the Customer object which can be accessed from the client side.
[WebMethod]
public Customer GetCustomerObject()
{
return new Customer();
}
Since, this method returns a Customer object the Customer type will be serialized and will be available on the client side.
Don't forget to rebuild and update the web references after making changes to the web service project.