0

My env is Asp.Net Core 2.1 EF Core 2.1

public class Customer
{
    public int Id { get; set; }

    public string name { get; set; }
    public virtual ICollection<CustomerLocation> CustomerLocations { get; set; }


public class CustomerLocation
{
    public int Id { get; set; }
    public int customerId { get; set; }
    public string streetAddress { get; set; }
    public string zipCode { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string category { get; set; }
    public virtual Customer Customer { get; set; }
}

In my Api controller

    // GET: api/Customers
    [HttpGet]
    public IEnumerable<Customer> GetCustomers()
    {
        var custlist = _context.Customers
            .Include(c=>c.CustomerLocations)
            .ToList();

        return custlist;

    }

and I would like to receive this JSON

[
{
    id: 1,
    name: "My First Company",
    customerLocations: [
    {
        id: 1,
        customerId: 1,
        streetAddress: "13 Union Street",
        zipCode: "94111",
        city: "San Francisco",
        state: "CA",
        category: "Headquarter",
        customer: null
    },
    {
        id: 2,
        customerId: 1,
        streetAddress: "1098 Harrison St",
        zipCode: "94103",
        city: "San Francisco",
        state: "CA",
        category: "Warehouse",
        customer: null
    }]
},
{
    id: 2,
    name: "Another Company",
    customerLocations: [ ]
}
]

but the answer i receive is

[
{
    id: 1,
    name: "My First Company",
    customerLocations: [
    {
        id: 1,
        customerId: 1,
        streetAddress: "13 Union Street",
        zipCode: "94111",
        city: "San Francisco",
        state: "CA",
        category: "Headquarter"

then it crashes trying to loop into the "customer" navigation property of "customerLocation".

The only way I found to get rid of this is to explicitly null all "customer" references in each CustomerLocation, but I can't believe this is the correct way to deal with this.

9
  • What is the exception? Commented Jul 29, 2018 at 6:00
  • @ofiris I can't get no exception. If I debug controller execution all is ok until the return statement is executed. Then in the browser I only have a JSON structure truncated just before the "customer" property. If I use Postman & Fiddler I get this response "[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 530 bytes." Commented Jul 29, 2018 at 7:05
  • You might found solution here stackoverflow.com/questions/2002940/… Commented Jul 29, 2018 at 7:20
  • @karan thank you for your reference, but it seems outdated (8 years ago); this has to be a EF core 2.1 problem, I did not have this problem with EF6. Commented Jul 29, 2018 at 7:31
  • 2
    While serializing your model , JSON.Net is finding a circular reference and breaks . You can either decorate "Customer" property in your CustomerLocation class with [JsonIgnore] attribute or use configuration described @ Stackoverflow post to handle this. Also, read this blog post for issues associated with both the approaches. Commented Jul 29, 2018 at 8:16

1 Answer 1

3

The reason of this error is Reference Loop while serializing Customer, and as you said when you set customer reference to null, you avoid the reference loop.

The other way that you can handle it is to set ReferenceLoopHandling for Json serializer in startup.cs

services
    .AddMvc()
    .AddJsonOptions(config =>
    {
        config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.