I have a class which has a many to many relationship with student. Please bare in mind this is a xarmain forms application talking to the client using NewtownSoft
public class Booking
{
public int Id { get; set; }
public int? DayOfWeek { get; set; }
public DateTime? BookingDate { get; set; }
public bool? IsAbsent { get; set; }
public DateTime? Time { get; set; }
public bool? HasCheckedIn { get; set; }
public ICollection<Student> Students { get; set; }
public bool? IsDeleted { get; set; }
public bool? IsActive { get; set; }
public string? CreatedBy { get; set; }
public string? LastModifiedBy { get; set; }
public DateTime? LastUpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
}
Student Class
public class Student
{
public int Id { get; set; }
public int? Type { get; set; }
public string? FirstName { get; set; }
public string? Surname { get; set; }
public DateTime? DOB { get; set; }
public decimal? Weight { get; set; }
public decimal? Height { get; set; }
public int? Gender { get; set; }
public string? Photo { get; set; }
public int? Age { get; set; }
public ICollection<Booking> Bookings { get; set; }
public bool? IsDeleted { get; set; }
public ICollection<Notes>? Notes { get; set; }
public decimal? TB { get; set; }
public decimal? OP { get; set; }
public decimal? PU { get; set; }
public decimal? PB { get; set; }
public decimal? BP { get; set; }
public bool? IsActive { get; set; }
public string? CreatedBy { get; set; }
public string? LastModifiedBy { get; set; }
public DateTime? LastUpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
}
I am adding that student to my api in the following way from the button click event.
private async void btnBookStudent_Clicked(object sender, EventArgs e)
{
//if we want the booking to include our student we must add it to our colleciton.
var test = Helpers.Dates.GetDateZeroTime(selectedBookingDate.Date).Add(timePicker.Time);
var student = await api.GetStudentById(StudentId);
var newBooking = new Booking
{
IsAbsent = false,
IsActive = true,
IsDeleted = false,
Time = Helpers.Dates.
GetDateZeroTime(selectedBookingDate.Date).
Add(timePicker.Time),
DayOfWeek = DayNumber
};
newBooking.Students = new List<Student>();
newBooking.Students.Add(student);
await api.AddToBooking(newBooking);
await DisplayAlert(Constants.AppName, "Booking Created For
Student", "OK");
}
However my client application is crashing out and not producing an error.
public async Task<HttpStatusCode> AddToBooking(Booking booking)
{
HttpStatusCode statusCode = new HttpStatusCode();
List<string> errors = new List<string>();
var serializerSettings = new JsonSerializerSettings {
ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Serialize};
string json =
JsonConvert.SerializeObject(booking,Formatting.Indented,
serializerSettings);
booking.CreatedBy = db.GetActiveUser();
var httpContent = new StringContent(json, Encoding.UTF8,
"application/json");
// AddAuthenicationHeader();
// Do the actual request and await the response
var httpResponse = await httpClient.PostAsync(Constants.BaseUrl Constants.ApiSegmant Constants.AddBooking, httpContent);
statusCode = httpResponse.StatusCode;
return statusCode;
}
As said before in my previous post its not giving me an error my Xamarin forms c# android application its just crashing at the JsonConvert line.
I have seen some articles suggesting turning off reference loop handling works but it doesn't in my case as I need to add the student at time of the booking.
How do I get more details error information on what is happening I tried adding.
On my booking class but it doesn't even get fired?. A try catch doesn't catch it either.
[OnError]
internal void OnError(StreamingContext context, ErrorContext errorContext)
{
var test = errorContext.Error;
}
I even tried [JsonIgnore] but i dont want that as I want the students to be with the bookings.
CodePudding user response:
There is a self-referencing loop, as both models reference each other and if Json.NET was to serialise the object, it'd be stuck between Booking
and Student
.
Try ignoring the bookings from being serialised in every student using [JsonIgnore]
.
public class Student
{
public int Id { get; set; }
public int? Type { get; set; }
public string? FirstName { get; set; }
public string? Surname { get; set; }
public DateTime? DOB { get; set; }
public decimal? Weight { get; set; }
public decimal? Height { get; set; }
public int? Gender { get; set; }
public string? Photo { get; set; }
public int? Age { get; set; }
[JsonIgnore]
public ICollection<Booking> Bookings { get; set; }
public bool? IsDeleted { get; set; }
public ICollection<Notes>? Notes { get; set; }
public decimal? TB { get; set; }
public decimal? OP { get; set; }
public decimal? PU { get; set; }
public decimal? PB { get; set; }
public decimal? BP { get; set; }
public bool? IsActive { get; set; }
public string? CreatedBy { get; set; }
public string? LastModifiedBy { get; set; }
public DateTime? LastUpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
}