471,595 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

circular reference problem

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

Nov 12 '05 #1
1 6344
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:
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

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vera | last post: by
6 posts views Thread by T Koster | last post: by
4 posts views Thread by Henke | last post: by
12 posts views Thread by Frank Rizzo | last post: by
6 posts views Thread by Stephen Robertson | last post: by
3 posts views Thread by Solution Seeker | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBIYWxl?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.