Why do we need serialization in a web service?

I have one web service:

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 asp.net application I consume it as:

     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 web method when I return custom types? when should we serialize? is it necessary at all, if so, what are the conditions?

+3
source share
1 answer

No, you donโ€™t have to do this. The execution engine will notice that you are returning your own type and serializing it in SOAP (! = XML) properly.

PS: Consider the transition to WCF

+2
source

Source: https://habr.com/ru/post/1750529/


All Articles