Setting Reference Loop Handling Service

A free video tutorial from Pavol Almasi
Computer Programmer at Berkshire Hathaway Company
16 courses
42,554 students
Learn more from the full course
Async API with Asp.Net Core & EF Core (step by step project)
Build complete async API from start to finish with .Net Core Services, AutoMapping, Interfaces, DTO objects and more
03:43:05 of on-demand video • Updated October 2020
How to build API from start to finish
Explore ASP .Net Core MVC
Apply REST to .Net Core API
Implement Data Transfer Objects
Configure .Net Core Services
Map Entitities with AutoMapper
Implement Relationships between classes
Work with Keys and Foreign Keys
English [Auto]
All right so let's round the project and see what happens and you can see that it loaded into a browser. Now we could use browser to actually make those HDTV requests. But when we are creating an API it's much better and much more productive to use a postman. So we of course will be using both meant for that by what I'm going to take if the local host number from it and then bring in the postman and here I will make the ACP beget request column. So we pass in the local host from the browser then API and then slash and that's events. This should give us all the events in the database. So let's see what happens and we are not getting events. In fact we are getting some strange error so let's see what's going on and what's going on is right here. It's unable to resolve servers for type comment events that services that I event repository while attempting to activate the comedy events controllers. So what's happening here is that we did not register the interface and the repository in our services. So we of course need to fix that. So I'm going to close this and we can go to our startup and here after the contact I'll add services dad and this time we are adding the I even repository along with the event repository that implements the interface. So we are going to use not just Ed but Ed sculpt. And like I said we are passing in the eye event repository which is implemented in the event repository. So it is registration for this service. But of course we need to bring in the name spaces which are in the event that services. So now if you're running again it should actually work. But before we do that I'm going to disable the browser from opening when we run the project because like I said we are going to use the postman only so when we click around project it will run. But without the browser being open and to do that I'll go to Social Explorer right click the comedy events go to properties and then in debug I will uncheck lounge browser and another thing I'm going to do is to specify a local host. It doesn't really matter what the number is. I just wanted something I can easily remember. So it's going to be let's say 5 7 0 0. That's going to be my local host so let's save all these changes and now let's run the project and the project started without the browser. So I go to my postman and I will send the GET request but I change the local host to 5 7 0 0 0 so that's going to be our HDTV request for local host. And then of course we have the slash API slash events. So let's click send and we are getting our events. You can see we have our event I.T. one because we only have one event along with the venue and the gigs are now because we did not specify that we also want gigs so that it was four gigs included equals false. So what happens when we do question mark and then include gigs equals true. So this is the parameter that should return the gigs. Along with this event let's go extend and you can see we are getting the events. And here we are getting the gigs but something's wrong. You can see it even indicates here that it's expecting Akamai instead of quotes. And what is happening here is called a reference looping. So let me go back to our controller here. Actually not even a controller to Social Explorer and open for example the event model. And you can see the event model has the venue in it as well as the collection of gigs. And when we go to gig it also has the event in it. So what is happening here that we have an event in both of these classes because they are related and that's the referential looping it tries to get the event from the gig. It goes to an event and then it tries to get the gig which also has the event. So it's basically just in a loop trying to reference each other and that's a fairly simple fix. Let's go to Social Explorer and open start up that's yes and here we have to set the reference looping to be ignored so in our services that we added the MVC we said the compatibility version and after that I'm going to also said the reference looping so I'll do another dad. And here I will add Jason options that will disable the reference looping. So I will add Jason options and in a lambda expression I'll do oh four options goes into oh God and we want to go to the serialize of settings that will serialize the Jason and then to the property of a reference looping or reference loop handling and the way we'll handle it is very simple will specify that Jason which is in the new Tom soft dad Jason Dad reference loop handling. So this property we will set to ignore and that will simply stop the looping from happening when two classes reference each other. So let's give it a try now. So my project growls let's just re around the get request and you can see everything is not formatted correctly. We have our event with the venue and all the information for the venue and those two gigs that are happening in that event as well as the comedians. So this is basically all the information for an event. We have the event venue gigs and comedian for each of the gigs. The question is do we really want all this information so to limit what is actually being returned we can create special classes called detail classes or transfer objects which will specify what properties of any of the given class we want to return in our requests. So let's create those detail classes next.