Fork me on GitHub

怎么解决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/

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

posted @ 2006-07-21 18:13  张善友  阅读(2772)  评论(2编辑  收藏  举报