asp.net - 为什么我们需要在 Web 服务中序列化

标签 asp.net web-services xml-serialization

我有一个网络服务:

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/

相关文章:

c# - 带有字符串列表的嵌套转发器

asp.net - 通过将 "userid"类型更改为整数来自定义 asp.net 成员资格

java - Eclipse (java) 网络服务,哪些文件要版本化以及如何将项目正确地拉到新计算机上?

c# - 根据我的 C# 类生成 xml 文件

c# - asp.net mvc 模型绑定(bind)不绑定(bind)集合

asp.net - 2010 年在 ASP.NET 中进行客户端/服务器验证的最佳方法是什么?

c# - REST HttpWebRequest GetResponseStream() 错误 500

xml - 如何防止 JSON Web 服务响应下载到文件?

c# - 更正 .NET 中 "mixed"类型的 XML 序列化和反序列化

c# - Xml序列化-直接渲染Root下的对象列表-Xml-Element