2009-09-23 67 views
0

我使用.net 3.5實現了一個小型Web服務。調用在.net 3.5中創建的Web服務時的循環引用問題

我已經使用linq to 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服務到我的本地在執行上面的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文件的時間有些serilization的問題。但我不知道如何解決這個問題。如果有人有解決方案,請儘快提供給我。在此先感謝...

回答

2

您需要更改數據模型中關聯的屬性,以便父屬性具有內部訪問修飾符。這將意味着只能在公共屬性上工作的XML序列化程序不會遵循循環引用。