When working with web services sometimes we need to return a collection of objects. If you are using the .NET framework 2.0 then you can simply return a strongly typed List<T>. If you are still stuck with .NET 1.0/1.1 then you can send back an ArrayList object. In this article we will look at how to send a list back to the web service client using .NET framework 1.0/1.1 and 2.0.
Introduction:
When working with web services sometimes we need to return a collection of objects. If you are using the .NET framework 2.0 then you can simply return a strongly typed List<T>. If you are still stuck with .NET 1.0/1.1 then you can send back an ArrayList object. In this article we will look at how to send a list back to the web service client using .NET framework 1.0/1.1 and 2.0.
Returning List<T> When Using .NET 2.0:
If you are using .NET 2.0 framework then you can simply return a generic list back to the client. The implementation of the WebMethod is shown below:
[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);
You will note that you will be able to access the Customer object which was created in the web service project. This is because you are returning List<Customer> from the WebMethod and it gets serialized on the way.
Returning ArrayList back to the Client:
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?
Since, ArrayList is not strongly typed the proxy generated has no idea what classes to create. You can however explicitly tell the web service to generate the classes by decorating the WebMethod with XmlInclude attribute.
[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(Customer))]
public ArrayList GetCustomerList()
{
ArrayList list = new ArrayList();
list.Add(new Customer("Mohammad", "Azam"));
return list;
}
The XmlInclude 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 scenario which you might encounter is when you need to access a Customer object and the web service only takes in or returns the simple 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.
Conclusion:
In this article we learned how to return custom collection of objects from the web service. We also looked back to the .NET 1.0/1.1 framework and discover various ways of solving this problem.