locked
[ASP.NET Core] OData Entity Set Called Infinitely Which Caused Connection Reset RRS feed

  • Question

  • User440671232 posted

    Hi All,

    Currently I need help on my OData, currently I've exposed several of my Code First models to OData, one of my entity set was called infinitely which cause the browser to return ERR_CONNECTION_RESET. I thought it caused by circular one to many navigation property (Entity A has list of Entity B and Entity B has 1 property of Entity A), but when I added another entity that has one to many relationship, this issue not occurred.

    Currently I have:

    1. Person (one to many with FamilyMembership)
    2. Family (one to many with FamilyMembership)
    3. FamilyMembership

    The problem is only happening with Person, when I tried to debug with Visual Studio, the Family only called once and the person called again and again.

    Thanks a lot.

    Thursday, June 22, 2017 12:08 PM

Answers

  • User440671232 posted

    mgebhard

    jazzyhacker

    Then I tried to make a custom class that has same properties with the Person class from the data layer and I can retrieve it successfully.

    That's called a View Model or a DTO and a common way to solve this issue.  Essentially, the entity is trying to fetch all the relationships from the database when the entity type is serialized.

    You can also use attributes to affect serialization.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization

    Hi Mgebhard,

    Thanks for your help, actually I just tried these data project on Web API 2 OData v4 and it worked perfectly, I believe this caused by ASP.NET Core OData support, the package that I used still in development:

    Microsoft.AspNetCore.OData

    So I will just use Web API 2 for OData until ASP.NET Core is ready.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 26, 2017 2:16 AM

All replies

  • User1967761114 posted

    Hi JazzyHacker,

    jazzyhacker

    The problem is only happening with Person, when I tried to debug with Visual Studio, the Family only called once and the person called again and again.

    According to your description, I’m unclearly sure what did you mean, could you provide more details describe about this issue?

    However, if you have some error with how to write the relationship in OData model, see the following code, it might help you.

    public class Person
    {
        public int Id
        {
            get; set;
        }
    
        public ICollection<FamilyMembership> FamilyMemberships { get; set; }
    }
    
    public class Family
    {
        public int Id
        {
            get; set;
        }
    
        public ICollection<FamilyMembership> FamilyMemberships { get; set; }
    }
    
    public class FamilyMembership
    {
        public int Id
        {
            get; set;
        }
    
        [ForeignKey("Person")]
        public int PersonId
        {
            get; set;
        }
    
        [ForeignKey("Family")]
        public int FamilyId
        {
            get; set;
        }
    
        public Family Family { get; set; }
    
        public Person Person { get; set; }
    }

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Friday, June 23, 2017 3:21 AM
  • User440671232 posted

    Hi JazzyHacker,

    jazzyhacker

    The problem is only happening with Person, when I tried to debug with Visual Studio, the Family only called once and the person called again and again.

    According to your description, I’m unclearly sure what did you mean, could you provide more details describe about this issue?

    However, if you have some error with how to write the relationship in OData model, see the following code, it might help you.

    public class Person
    {
        public int Id
        {
            get; set;
        }
    
        public ICollection<FamilyMembership> FamilyMemberships { get; set; }
    }
    
    public class Family
    {
        public int Id
        {
            get; set;
        }
    
        public ICollection<FamilyMembership> FamilyMemberships { get; set; }
    }
    
    public class FamilyMembership
    {
        public int Id
        {
            get; set;
        }
    
        [ForeignKey("Person")]
        public int PersonId
        {
            get; set;
        }
    
        [ForeignKey("Family")]
        public int FamilyId
        {
            get; set;
        }
    
        public Family Family { get; set; }
    
        public Person Person { get; set; }
    }

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Hi EvenMa,

    Thanks a lot for our reply, actually my code is exactly the same with the code you gave me, I placed it in another project (Data Layer) and referenced it to my Web App project that contains this OData Service. Currently, I have 3 Web APIs (Person, Family, FamilyMembership) that use OData, they only have 1 get method to retrieve all data from database and I marked them with Enable Query Attribute. I can called Family and Family Membership OData successfully but not with the Person Controller. The browser returned ERR_CONNECTION_RESET. So I tried to debug the web app and it appeared that the Person controller is hit over and over again when I try to navigate from the browser which I believe cause the ERR_CONNECTION_RESET, because the browser detects an infinite loop request. Then I tried to make a custom class that has same properties with the Person class from the data layer and I can retrieve it successfully. Do you have any idea how to solve this issue? Is there any mistake in my configuration?

    Thanks.

    Friday, June 23, 2017 10:49 AM
  • User475983607 posted

    Then I tried to make a custom class that has same properties with the Person class from the data layer and I can retrieve it successfully.

    That's called a View Model or a DTO and a common way to solve this issue.  Essentially, the entity is trying to fetch all the relationships from the database when the entity type is serialized.

    You can also use attributes to affect serialization.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization

    Friday, June 23, 2017 11:15 AM
  • User440671232 posted

    mgebhard

    jazzyhacker

    Then I tried to make a custom class that has same properties with the Person class from the data layer and I can retrieve it successfully.

    That's called a View Model or a DTO and a common way to solve this issue.  Essentially, the entity is trying to fetch all the relationships from the database when the entity type is serialized.

    You can also use attributes to affect serialization.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization

    Hi Mgebhard,

    Thanks for your help, actually I just tried these data project on Web API 2 OData v4 and it worked perfectly, I believe this caused by ASP.NET Core OData support, the package that I used still in development:

    Microsoft.AspNetCore.OData

    So I will just use Web API 2 for OData until ASP.NET Core is ready.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 26, 2017 2:16 AM