Xamarin表单应用程序在SerializeObject未产生错误时崩溃



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

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

学生类

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

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

我看到一些文章建议关闭参考循环处理工作,但在我的情况下没有,因为我需要在预订时添加学生。

我如何获得更多的细节错误信息发生了什么我尝试添加。

在我的预定课上,但它甚至没有被解雇。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; }
} 

最新更新