This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, I am working with Asp.net application including Ordinary Web Service with your framework.In Model.Ismodel , we have Customer ,SalesOrderHeader,SalesOrderDetail table .When i try to query the Customer details with 10 records with Lazy Loading .Based on foreign key , it is fetching the SalesOrderDetail as well as SalesOrderHeader record. We getting the result . But When we try to return the object in the web method , we got Circular reference error When try to serialize the object. Here is code .
[WebMethod] var uow = _context.CreateUnitOfWork();
ErrorMessage: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: A circular reference was detected while serializing an object of type LightSpeedFrameWork.SalesOrderHeader. at System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name, String ns, Object o, Boolean writePrefixed, XmlSerializerNamespaces xmlns) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_SalesOrderHeader(String n, String ns, SalesOrderHeader o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_SalesOrderDetail(String n, String ns, SalesOrderDetail o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_SalesOrderHeader(String n, String ns, SalesOrderHeader o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write8_Customer(String n, String ns, Customer o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write13_ArrayOfCustomer(Object o) at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfCustomerSerializer.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() can u provide the solution for this. Thanks and regard , Upendran P.V. |
|
|
Hi, This is because of a limitation in the XML serializer which you are using because you will have a circular reference in your model. Since I cant see your model in the example above, lets say that your Customer has a relationship to an Order. e.g. a Customer has many Orders (an EntityCollection of Orders) and an Order has a Customer (an EntityHolder of Customer). The XML serializer will walk the object graph trying to serialize any child objects of the entity you are dealing with. So it will start to serialize a Customer and then see that the Customer has Orders. So it will start to serialize those Orders. For the first order that it finds it will see that the Order has a Customer. It will then start to serialize the Customer. At this point it will realize that it has seen the customer before and throw the exception you are seeing. This is a limitation of the default XML serializer in that it cannot handle circular references in object graphs that it is serializing. My suggestion would be to look at a couple of available options:
We do have designer support to generate DTOs and some simple mapping helper functions for you if you are using WCF. You can find more details on this here: http://www.mindscape.co.nz/blog/index.php/2009/01/28/dtos-data-contacts-and-code-generation/
Jeremy |
|
|
Hi jeremy, Thank you for quick reply.Here we doing Eager Loading. As per your information, we had worked with WCF service.Its working perfectly by returning public List<CustomerDto> GetCustomerDetail() But we want to return the CustomerDto along with SalesOrderHeaderDto and SalesOrderDetailDto(using a single object to bind in the Grid at client side). (i.e) Instead of returning "customerList" we want to return "customers"(with SalesOrderHeader+SalesOrderDetail) is it possible to return single object(with Customer+SalesOrderHeader+SalesOrderDetail).If it is possible , can u send some sample code. Thanks in Advance, Upendran P.V. |
|
|
This is more of a general .NET question; but to handle this you will want to set up a seperate class to return this structure and then map your objects into it accordingly. So your method could become.. CustomerResult[] GetCustomerDetail()
where CustomerResult would be defined as..
[DataContract] public class CustomerResult { [DataMember] CustomerDto Customer { get; set; } [DataMember] SalesOrderHeaderDto[] SalesOrderHeaders { get; set; } [DataMember] SalesOrderDetailDto[] SalesOrderDetails { get; set; } } |
|