Web Api (MVC 6) Chunked body did not terminate properly with 0-sized chunk

asp.net-core-mvc asp.net-mvc asp.net-web-api c# entity-framework-core


I am using MVC 6 rc1 with EF 7 rc 1 Code First Model to retrieve data over web api controller. I have 3 tables similar to below.

class Product
    public int Id { get; set; }
    public string SomeProperty { get; set; }
    public virtual ICollection<Categorization> Categorizations { get; set; }
    public DateTime SomeProperty2 { get; set; }
    public string SomeProperty3 { get; set; }
    public string SomeProperty4 { get; set; }


// NOTE: Entity key should be (ProductId, CategoryId)
class Categorization
    public int ProductId { get; set; }
    public Product Product { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }

class Category
    public int Id { get; set; }
    public ICollection<Categorization> Categorizations { get; set; }

My Controller:

        public IEnumerable<Product> searchProducts(string searchText,int? id)
          var ret= db.Products
                .Include(s => s.Categorizations).Take(2).ToList();
          return ret;

Below is my Startup.cs ConfigureServices section.

                    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    options.SerializerSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;

            var connectionString = Configuration.GetSection("Data:DefaultConnection:ConnectionString").Value;

                .AddDbContext<ContractsContext>(options => options.UseSqlServer(connectionString));

            services.AddSingleton(_ => Configuration);
            services.AddSingleton<IContractRepository, ContractRepository>();

When I call the api, I get the error as "Chunked body did not terminate properly with 0-sized chunk" in Fiddler. In fiddler resultset, I see only the first object of expected result set with properties until Categorizations filled and NO properties and remaining objects after that (Incomplete JSON data). If I do not include Categorizations in the result set, it works perfectly fine. Am I missing something? Note: EF is returning data correctly but it's getting chunked in the api call and client is unable to read the data in complete.

12/9/2015 6:03:27 PM

Popular Answer

Found the issue. Self referencing loop detected for property 'Product' with type 'Product'. Path '[0].Categorizations[0]'.

So, EF fills out both Categorization collection in Product object and also Product object in Categorization. So, while serializing to json it became a infinite loop, like:

Product>Categorizations(eachCategorization - Product>Categorizations(eachCategorization - Product>Categorizations(eachCategorization - Product>Categorizations(....

Solution: Change Startup.cs ConfigureServices section as below

                .AddJsonOptions(options =>
                    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
12/10/2015 6:36:34 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow