views:

43

answers:

1

I have a database with a lots of relationships between Tables and a Silverlight client that connects to my server with WCF service on ASP.Net side.

First i used LINQ to SQL as a robust mapper tables to object and in a WebMethod that returns a List<Foo> of my Database's object(suppose GetFoo()). The Foo has lots of relationships with other objects that each of that have lots of realaships too,(this means , there is a PK and FK between tables).also i use Microsoft Service Trace Viewr for track my service

When i call GetFoo() , WCF returns this error:

Object graph for type 'X.Y.Z' contains cycles and cannot be serialized if reference tracking is disabled

I searched this error and find this great post but that is not working properly and i see same error too.

+1  A: 

Various options:

  • remove the cyclic dependencies from your model; this might be tricky for a generated model that has lots of existing code built against it, but is worth a try; however, you typically want to not serialize the parent, which is exactly what LINQ-to-SQL wants you to keep (it'll let you drop the children property, but that is what you usually want to serialize)
  • enable cyclic references; it looks like you've tried this without success; did you enable it at both ends, though? Actually I wouldn't be surprised if Silverlight doesn't like this extension (it has limited extension support)
  • use a separate (flat) DTO model for data transfer purposes
  • try using NetDataContractSerializer; I can't remember if this is supported in Silverlight, and I must admit I'm not its biggest fan, but it might be a pragmatic fix here

I'd vote firmly in the "DTO model" category; simply, having a separate model means you are less likely to run into tangles whenever you tweak the DB - and you are in complete control over it.

Marc Gravell
@Marc Gravell: i can't remove cyclic dependencies , it's lot! have any code to detect it? if i want to use DTO i must create and implement more than 200 object and i have deadline!
Meysam Javadi
@Marc Gravell: enable cyclic references? how to do in ends?
Meysam Javadi
@Meysam - like in the blog article you linked to; but it needs to apply at both the client and server. I haven't tried that in silverlight, so I can't comment on whether it is possible.
Marc Gravell
@Marc Gravell: and how to find cyclic dependencies automatically? is there any way?
Meysam Javadi
@Meysam - well, you could write something with reflection fairly easily that simply walks the properties
Marc Gravell
@Marc Gravell: oh no, not a hand written!some tools or...
Meysam Javadi