Xamarin 表单应用程序在 SerializeObject 处崩溃不产生错误

Xamarin form application crashing at SerializeObject not producing error

我有一个 class 与学生有多对多的关系。请记住,这是一个使用 NewtownSoft

与客户端对话的 xarmain 表单应用程序
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; }
}

学生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; }

}

我通过以下方式从按钮单击事件中将该学生添加到我的 api。

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");
 
}

但是我的客户端应用程序崩溃并且没有产生错误

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;

   }

正如我之前所说 post 我的 Xamarin 表单 c# android 应用程序没有给我一个错误,它只是在 JsonConvert 行崩溃。

我看过一些文章建议关闭参考循环处理,但在我的情况下并不适用,因为我需要在预订时添加学生。

如何获取有关我尝试添加的情况的更多详细错误信息。

在我的预订中 class 但它甚至没有被解雇?。 try catch 也抓不到。

[OnError]
internal void OnError(StreamingContext context, ErrorContext errorContext)
{
      var test = errorContext.Error;
}

我什至尝试了 [JsonIgnore],但我不希望那样,因为我希望学生参与预订。

有一个自引用循环,因为两个模型相互引用,如果 Json.NET 是序列化对象,它会卡在 BookingStudent 之间。

尝试忽略使用 [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; }

}