Connecting Tech Pros Worldwide Forums | Help | Site Map

circular reference problem

dotnetnewbie
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi all

I am new to .NET and webservice so I wish someone can shed some light
on me.

I have a Project class and a Product class, the Project can contain
multiple Products (as an ArrayList). In my WebMethod I Initialize my
project as following:


<WebMethod()> Public Function ProjectInitial()
As Project

'Public Function ProjectInitial(ByVal inputProduct As
Product) As Project
Dim myProject As New Project
myProject = InitialProject("Test Project")

Dim myProduct As New Product
myProduct.cost = 100
myProduct.masterQty = 1000

Dim myProduct2 As New Product
myProduct2.cost = 200
myProduct2.masterQty = 2000
myProject.AddFinishProduct(myProduct)
myProject.AddFinishProduct(myProduct2)

Return myProject
End Function


The following is the structure of my Project class

[code:1:bb159c481b]

Imports System.Xml.Serialization

<XmlInclude(GetType(Product))> Public Class
Project

#Region " Project Properties "

Public projectID As Int32
Public projectName As String
Public projectStatus As Int32
Public projectCreateDate As DateTime
Public projectModifyDate As DateTime

Public ProductList As New ArrayList

#End Region

Public Sub AddFinishProduct(ByVal inputProduct As
Product)
'Add Product into the arraylist "ProductList "
....
....
End Sub
End Class
[/code:1:bb159c481b]

The following is the structure of my Product class

[code:1:bb159c481b]

Imports System.Xml.Serialization

<XmlInclude(GetType(Properties)),
SoapInclude(GetType(Product))> Public Class
Product
' Product properties
Public masterQty As Int32
Public cost As Double ' temperary cost, will use config costs to
replace
Public PropertyList As New ArrayList

' ArrayList stores it's component product
Public componentProduct As New ArrayList

Public Sub AddProduct(ByVal inputProduct As Product)
'Add Product object into componentProduct
....
....
End Sub
End Class
[/code:1:bb159c481b]

As you can see the I wish the Product can also contain a list of
Products because in my logic some Product can be constructed by a
list of Component Product but I want them to be in the same structure
because they have the same properties, also a Component Product can be
constructed by other Component Product.

If I don't add Product object into the Product, every
thing goes ok. But if I add a Project object inside a Product I have
the following error message

System.InvalidOperationException: There was an error
generating the XML document. --->
System.InvalidOperationException: A circular reference was detected
while serializing an object of type
ProjectInitialize.Product.

Is there any work around I can do to solve the problem? :?

Thanks in advance for your tips


Benjamin Day
Guest
 
Posts: n/a
#2: Nov 12 '05

re: circular reference problem


Has anyone found a solution to this web service circular reference problem?

Here's a simplified example:

Person him=new Person();
Person her=new Person();

him.Friend=her;
her.Friend=him;

Now try to serialize one of those object through an ASMX web service and
you'll get the following:

System.InvalidOperationException: There was an error generating the XML
document. --->
System.InvalidOperationException: A circular reference was detected while
serializing an object of type Person.

This article on IBM's site implies that .NET can do this somehow:
http://www-106.ibm.com/developerwork...-tip-j2eenet1/

Any ideas?

-Ben
http://www.benday.com



"dotnetnewbie" wrote:
[color=blue]
> Hi all
>
> I am new to .NET and webservice so I wish someone can shed some light
> on me.
>
> I have a Project class and a Product class, the Project can contain
> multiple Products (as an ArrayList). In my WebMethod I Initialize my
> project as following:
>
>
> <WebMethod()> Public Function ProjectInitial()
> As Project
>
> 'Public Function ProjectInitial(ByVal inputProduct As
> Product) As Project
> Dim myProject As New Project
> myProject = InitialProject("Test Project")
>
> Dim myProduct As New Product
> myProduct.cost = 100
> myProduct.masterQty = 1000
>
> Dim myProduct2 As New Product
> myProduct2.cost = 200
> myProduct2.masterQty = 2000
> myProject.AddFinishProduct(myProduct)
> myProject.AddFinishProduct(myProduct2)
>
> Return myProject
> End Function
>
>
> The following is the structure of my Project class
>
> [code:1:bb159c481b]
>
> Imports System.Xml.Serialization
>
> <XmlInclude(GetType(Product))> Public Class
> Project
>
> #Region " Project Properties "
>
> Public projectID As Int32
> Public projectName As String
> Public projectStatus As Int32
> Public projectCreateDate As DateTime
> Public projectModifyDate As DateTime
>
> Public ProductList As New ArrayList
>
> #End Region
>
> Public Sub AddFinishProduct(ByVal inputProduct As
> Product)
> 'Add Product into the arraylist "ProductList "
> ....
> ....
> End Sub
> End Class
> [/code:1:bb159c481b]
>
> The following is the structure of my Product class
>
> [code:1:bb159c481b]
>
> Imports System.Xml.Serialization
>
> <XmlInclude(GetType(Properties)),
> SoapInclude(GetType(Product))> Public Class
> Product
> ' Product properties
> Public masterQty As Int32
> Public cost As Double ' temperary cost, will use config costs to
> replace
> Public PropertyList As New ArrayList
>
> ' ArrayList stores it's component product
> Public componentProduct As New ArrayList
>
> Public Sub AddProduct(ByVal inputProduct As Product)
> 'Add Product object into componentProduct
> ....
> ....
> End Sub
> End Class
> [/code:1:bb159c481b]
>
> As you can see the I wish the Product can also contain a list of
> Products because in my logic some Product can be constructed by a
> list of Component Product but I want them to be in the same structure
> because they have the same properties, also a Component Product can be
> constructed by other Component Product.
>
> If I don't add Product object into the Product, every
> thing goes ok. But if I add a Project object inside a Product I have
> the following error message
>
> System.InvalidOperationException: There was an error
> generating the XML document. --->
> System.InvalidOperationException: A circular reference was detected
> while serializing an object of type
> ProjectInitialize.Product.
>
> Is there any work around I can do to solve the problem? :?
>
> Thanks in advance for your tips
>
>[/color]
Closed Thread