2010-06-17 34 views
3

我有一个Web服务:为什么我们需要序列化的Web服务

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; 
    } 

,并在asp.net应用程序,我消费它想:

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

现在我的问题是,我需要序列化我的webmethod,因为我返回自定义类型?我们应该什么时候序列化?是否有必要,如果是的话,那么条件是什么?

回答

2

不,你不必这样做。执行引擎会注意到您返回自定义类型并将其序列化为SOAP(!= XML)。

PS:考虑搬到WCF

+0

是的,我也在学习wcf。 所以当我们需要序列化的时候,你能否解释一下。 – Cloud2010 2010-06-17 16:37:19

+0

@ Cloud2010:在我们不需要Web Service/WCF的情况下,引擎会为我们做这件事。当我们想要以某种自定义的方式存储或传输对象时,需要序列化。例如,如果我们想将对象保存到文件。 – Andrey 2010-06-17 16:39:57

+0

发动机?你在谈论ASP.net引擎 – Cloud2010 2010-06-17 16:42:41

相关问题