我正在尝试解析Web服务中的循环引用。解决方案构建良好,一些操作是可调用的,但使用引用类型的操作会给出循环引用异常(下面)。
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: A circular reference was detected while serializing an object of type MyCoreAssembly.MyType <-------- this is the referenced type
at System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name, String ns, Object o, Boolean writePrefixed, XmlSerializerNamespaces xmlns)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write21_VRD(String n, String ns, VRD o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write21_VRD(String n, String ns, VRD o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write31_Hereditament(String n, String ns, Hereditament o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write32_HereditamentVrd(String n, String ns, HereditamentVrd o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write33_VRDResponse(String n, String ns, VRDResponse o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write43_VRDResponse(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.VRDResponseSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer) <------- this is my type
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.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()
架构看起来大致如下:
Core layer <------ Data Layer <----- Logic Layer <---------- Web service
\/--------------------\/ <-------------------|
\/--------------------|
每个图层都是一个单独的程序集,Web服务引用所有三个程序集。我附加了一个调试器,但它没有破坏。它看起来像内部失败但我不知道一切都在哪里构建。我该如何调试呢?
答案 0 :(得分:4)
您的循环引用不在项目文件中,而是在您尝试通过Web服务发送的对象中。其中一个对象具有对自身的引用或引用该对象的对象。
错误消息告诉您查看类型: MyCoreAssembly.MyType
答案 1 :(得分:0)
引用自身的类应使用DataContract属性进行修饰。将重载IsReference =设置为true将启用循环引用。 [DataContract(IsReference =真)]