I am building a website in ASP.NET MVC4 the application is linked with a webAPI. in both application and API I use a dbml LINQ to SQL file for the db connection and also serializing and deserializing. The application has no active db connection, so the dbml is only used for deserializing purposes.
Results from the webAPI are serialized by JSON.NET, which are always successfull. Example:
[{"$id":"1","ID_DepartmentSlot":2400,"SlotStart":"2013-04-29T21:00:00","SlotEnd":"2013-04-30T01:00:00","Created":"2013-04-29T11:00:40.837","Deleted":false,"ID_Staff":19,"ID_User":1,"SlotRemark":"test other day","Approved":true,"Notifications":[],"Staff":{"$id":"2","ID_Staff":19,"StaffDescription":"MUG-arts","Created":"2013-03-26T13:21:51.407","Deleted":false,"ID_Department":1,"Staff_Users":[],"DepartmentSlots":[]}}]
The problem arizes when I try to deserialize this JSON in the application. I get the known "Circular reference" error.
Options in the webAPI:
wachtapplicatieDBDataContext db = new wachtapplicatieDBDataContext();
db.ObjectTrackingEnabled = false;
var dataoptions = new System.Data.Linq.DataLoadOptions();
dataoptions.LoadWith<DepartmentSlot>(ds => ds.Staff);
In both API and application I use following serializing options:
JsonFormatter.MaxDepth = 2;
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Newtonsoft.Json.PreserveReferencesHandling.Objects;
Newtonsoft.Json.Formatting.None;
Newtonsoft.Json.NullValueHandling.Ignore;
Newtonsoft.Json.ObjectCreationHandling.Replace;
Newtonsoft.Json.MissingMemberHandling.Ignore;
Also, I don't get this problem on all deserialisation methods, only specific.. But i don't see where the problem rises... because the methods are all alike.