• 怎么解决web service circular reference 问题



    下面是一个非常简单的例子:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    namespace WebService1
    {

        [Serializable]
        public class Person
        {
            private Person friend;
            private String name;

            public String Name
            {
                get { return name; }
                set { name = value; }
            }

            public Person Friend
            {
                get { return friend; }
                set { friend = value; }
            }
        }
    }

    using System;
    using System.Data;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.ComponentModel;

    namespace WebService1
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class Service1 : System.Web.Services.WebService
        {

            [WebMethod]
            public Person HelloWorld()
            {
                Person him = new Person();
                Person her = new Person();

                him.Friend = her;
                her.Friend = him;
                return him;
            }
        }
    }


    运行 ASMX web service 将会得到下面的错误:

    System.InvalidOperationException: There was an error generating the XML
    document. --->
    System.InvalidOperationException: A circular reference was detected while
    serializing an object of type Person.

    IBM 网站有一篇文章描述了这个问题:
    http://www-106.ibm.com/developerwor...s-tip-j2eenet1/

    各位兄弟,谁解决过这个问题

    欢迎大家扫描下面二维码成为我的客户,为你服务和上云

  • 相关阅读:
    STL--set_difference
    priority_queue和sort应用
    16进制转化8进制---map
    LeetCode62. 不同路径
    LeetCode209. 长度最小的子数组
    LeetCode第29场双周赛题解
    LeetCode61. 旋转链表
    LeetCode60. 第k个排列
    LeetCode59. 螺旋矩阵 II
    LeetCode58. 最后一个单词的长度
  • 原文地址:https://www.cnblogs.com/shanyou/p/456700.html
Copyright © 2020-2023  润新知