怎么解决web service circular reference 问题

2018-01-29 11:45:14 浏览数 (1)

下面是一个非常简单的例子:

代码语言:js复制
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 将会得到下面的错误:

代码语言:js复制
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/

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

0 人点赞