views:

43

answers:

1

I have one webservice:

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
}


public class Service : System.Web.Services.WebService
{
    public Service () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public List<Product> GetItems()
    {
        List<Product> productList = new List<Product>()
        {
            new Product{ProductId=1,ProductName="Pencil"},
            new Product{ProductId=2,ProductName="Pen"}
        };

        return productList;
    }

and in a asp.net application I am consuming it like:

     localhost.Service s = new localhost.Service();
    List<localhost.Product> k = new List<localhost.Product>();
    k = s.GetItems().ToList();  // i am getting the values here.

now my question is do I need to serialize my webmethod as i am returning custom types? when should we serialize ? is it necessary at all, if yes , then what are the conditions?

+1  A: 

No, you don't have to do it. Executing engine will notice that you return custom type and serialize it into SOAP ( != XML ) properly.

PS: Consider moving to WCF

Andrey
yes, i am also learning wcf.so when exactly we need serialization, can u explain a bit.
Cloud2010
@Cloud2010: in case of Web Service / WCF we don't need, the engine does it for us. Serialization is needed when we want to store or transfer objects in some custom way. For example if we want to save object to file.
Andrey
Engine? are you talking about ASP.net engine
Cloud2010
@Cloud2010: yes, about it. in case of WCF, you have to use `DataContracts` it is more explicit, but still the actual serialization is done by WCF engine.
Andrey