我有一个网络服务:
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?我们什么时候应该序列化?有没有必要,如果有的话,条件是什么?
最佳答案
不,你不必这样做。执行引擎将注意到您返回自定义类型并将其正确序列化为 SOAP (!= XML)。
PS:考虑迁移到 WCF
关于asp.net - 为什么我们需要在 Web 服务中序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3063570/