开发者

Why do we need serialization in web service

开发者 https://www.devze.com 2023-01-03 16:06 出处:网络
I have one webservice: public class Product { public int ProductId { get; set; } public string ProductName { get; set; }

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,ProductNam开发者_开发技巧e="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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号