循环引用调用在.net 3.5中创建的Web服务时出现问题

时间:2009-09-23 10:00:18

标签: asp.net web-services linq-to-sql

我使用.net 3.5实现了一个小型Web服务。

我已经将linq用于sql类来访问数据库。

我写了以下webmethods如下:

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;

...


    [WebMethod]
    public List<restaurant_detail> restaurantDetailList()
    {
        return dc.restaurant_details.ToList();
    }

这里我使用LINQ从数据库中获取数据。现在,当我将此web服务执行到我的localhost时,我在执行上述web方法时遇到以下错误:

   System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: A circular reference was detected while serializing an object of type restaurant_cuisine.
   at System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name, String ns, Object o, Boolean writePrefixed, XmlSerializerNamespaces xmlns)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_restaurant_cuisine(String n, String ns, restaurant_cuisine o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_cuisine(String n, String ns, cuisine o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_restaurant_cuisine(String n, String ns, restaurant_cuisine o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write14_restaurant(String n, String ns, restaurant o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write15_restaurant_detail(String n, String ns, restaurant_detail o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write17_ArrayOfRestaurant_detail(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfrestaurant_detailSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()

我在解析xml文件时遇到了一些问题。但我不知道如何解决这个问题。如果有人有解决方案,请尽快提供给我。提前谢谢......

1 个答案:

答案 0 :(得分:2)

您需要更改数据模型中关联的属性,以便父属性具有内部访问修饰符。这意味着只能在公共属性上运行的XML序列化程序不会遵循循环引用。