A circular reference was detected while serializing an object of type 'XXX'

I ran into an issue earlier when trying to return an entity class through a web service for use in an AJAX page. After some quick research, I came across a post by Darren Neimke that referenced Rick Strahl's post about LINQ to SQL and Serialization. For me, performing the first work-around and setting the relationship to 'Friend' worked.

Thanks Rick and Darren!

Published Monday, June 09, 2008 4:43 PM by Jason N. Gaylord

Comments

# re: A circular reference was detected while serializing an object of type 'XXX'

Monday, June 09, 2008 9:11 PM by Darren Neimke

No worries Jason! :-)

# re: A circular reference was detected while serializing an object of type 'XXX'

Tuesday, January 13, 2009 8:29 AM by anil

To avoid this kind of error, you need to select each columns individually as follows.

var result = (from p in sqlD.Tracks

                                 where (p.Title.Contains(Request["searchText"].ToString())  || p.Tags.Contains(Request["searchText"].ToString()) || p.Description.Contains(Request["searchText"].ToString()))

                                 && p.Status == true

                                 orderby p.CreateDate descending

                                 select new

                                 {

                                     TrackId = p.ID,

                                     TrackTitle = p.Title,

                                     TrackDesc = p.Description,

                                     TrackTags = p.Tags,

                                     TrackPrice = p.Price,

                                     TrackPhoto = p.TrackPhoto

                                 }

                                  ).ToList();

                   return "{\"Table\" : " + result.ToJson() + "};";

I hope this will help you.

Thanks

<a href="http://www.ade-technologies.com">Anil</a>

# re: A circular reference was detected while serializing an object of type 'XXX'

Thursday, April 09, 2009 9:33 PM by Bangoker

Or, select all and then give a NULL value to those properties, they will not pass along in the xml/soap message, as if they didnt exist :P, but im going to try the Friends one too

# re: A circular reference was detected while serializing an object of type 'XXX'

Thursday, May 07, 2009 5:07 AM by srinivasareddy

www.west-wind.com/.../147218.aspx

# re: A circular reference was detected while serializing an object of type 'XXX'

Wednesday, May 27, 2009 4:50 PM by greg

I think you guys mean "internal". The term "friend" is not used in the linked article.

# re: A circular reference was detected while serializing an object of type 'XXX'

Thursday, September 17, 2009 1:28 PM by Kalyan Revadi

I ran into the same problem and THE ONE OF SOLUTION IS to

Use [ScriptIgnore] atribute .. it will solve the problem.

add system.web.extensions reference and add namespace:

Using System.Web.Script.Serialization.

If you still have questions..Read on..for some detailed explanation..

i have a User class with..

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Iesi.Collections.Generic;

using System.Runtime.Serialization;

namespace RAPortal.Core.Entities

{

   public class User

   {

       private int _userId;

       private string _firstName;

       private string _lastName;

private IList<Application> _applications;

       private IList<Group> _groups;

       private IList<ApplicationRequest> _applicationRequests;

# re: A circular reference was detected while serializing an object of type 'XXX'

Thursday, September 17, 2009 1:35 PM by Kalyan Revadi

CONTINUED.....

the Properties..

       public virtual int UserId

       {

           get

           {

               return _userId;

           }

           set

           {

               _userId = value;

           }

       }

       public virtual string Title

       {

           get

           {

               return _title;

           }

           set

           {

               _title = !String.IsNullOrEmpty(value) ? value.ToUpper().Trim() : null;

           }

       }

public virtual IList<Group> Groups

       {

           get

           {

               return _groups;

           }

           set

           {

               _groups = value;

           }

       }

       public virtual IList<UserPrivilege> UserPrivileges

       {

           get

           {

               return _userPrivileges;

           }

           set

           {

               _userPrivileges = value;

           }

       }

       public virtual IList<UserRole> UserRoles

       {

           get

           {

               return _userRoles;

           }

           set

           {

               _userRoles = value;

           }

       }

...so on...

and I have Groups class..

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Script.Serialization;

using System.Runtime.Serialization;

namespace RAPortal.Core.Entities

{

   public class Group

   {

       private int _groupId;

       private string _name;

       private IList<User> _users;

       public virtual int GroupId

       {

           get

           {

               return _groupId;

           }

           set

           {

               _groupId = value;

           }

       }

       public virtual string Name

       {

           get

           {

               return _name;

           }

           set

           {

               _name = !String.IsNullOrEmpty(value) ? value.ToUpper().Trim() : null;

           }

       }

       [ScriptIgnore]

       public virtual IList<User> Users

       {

           get

           {

               return _users;

           }

           set

           {

               _users = value;

           }

       }

   }

}

Since User is referenced in the groups.. the json think that it is Circular reference and It will throw an exception..so the fix is to add [ScriptIgnore] on top of the User. And add the reference and namespace to this class like it..

It solved my problem .. I am sure there are better ways out there !!! Cheers...

And remember you should add [scriptIgnore] only in the groups class and not in the Users class..

Leave a Comment

(required) 
(required) 
(optional)
(required)