0

I have two model classes ServiceProvider.cs & Club.cs one service provider can have many clubs in it. However ServiceProvider.cs has a virtual collection of clubs in it. As shown below

public class ServiceProvider 
{
    public int ServiceProviderId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNo { get; set; }

    public virtual ICollection<Club> Clubs { get; set; }
}
public class Club 
{
    public int ClubId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public virtual ServiceProvider serviceProvider { get; set; }
    public int ServiceProviderId { get; set; }
}

I have implemented a repository pattern to query the MS Sql server, if i return my repository's _serviceProvider GetAll() method it works fine, and i get all the serviceprovider objects in PostMan.

public IEnumerable<ServiceProvider> Get()
    {
        return _serviceProvider.GetAll();
    }

But if i return the GetAllWithClubs() method of repository then i get run time exception here. Problem is: I am unable to get all service providers along with their clubs collection. However, i have already debugged my code before putting question here, I am getting all the service providers along with their clubs collection perfectly fine. I am sure data fetching from database is fine, problem coming in sending response back from Get() Action method.

public IEnumerable<ServiceProvider> Get()
    {
        return _serviceProvider.GetAllWithClubs();
    }

Error i am getting in Postman is enter image description here output in visual studio is enter image description here

I have tried searched solution of .toList() in front of GetAllWithClubs() plus returning ActionResult which came with Asp.net core 2.1 (Not supported in my case 2.0). I believe this is not a duplicate question. Implementation of GetAll() and GetAllWithClubs() methods are below.

public IEnumerable<ServiceProvider> GetAllWithClubs()
    {
        return _context.ServiceProviders
            .Include(c => c.Clubs);
    }

In Repository.cs

public IEnumerable<T> GetAll()
    {
        return _context.Set<T>();
    }
Shahjahan KK
  • 91
  • 1
  • 3
  • 12
  • 1
    What is the exception? –  Aug 21 '18 at 08:59
  • @Stephen Muecke i have edited my question, kindly review now. – Shahjahan KK Aug 21 '18 at 09:02
  • 1
    Your question still not state what the exception is –  Aug 21 '18 at 09:03
  • @StephenMuecke updated question with error – Shahjahan KK Aug 21 '18 at 09:08
  • @ShahjahanKK "GetAllWithClubs() method of repository then i get run time exception here." which error return in you question you provide error of postman but what exactly error throw from your repository please mention that. – Lalji Dhameliya Aug 21 '18 at 09:23
  • @LaljiDhameliya could you kindly check now, i have edited my question with vs error. – Shahjahan KK Aug 21 '18 at 09:35
  • you need to start debugging your own code, put some breakpoints, see what data you get back when calling GetAll. Do you hit the endpoint at all? Do you get data back? There is a lot you can do on your end before throwing in the towel – Andrei Dragotoniu Aug 21 '18 at 10:06
  • @AndreiDragotoniu You Sir, i have already debugged my code before sending in here, I am getting all the service providers along with their clubs collection perfectly fine. i think data fetching from database is fine, problem coming in send response back from Get() Action method – Shahjahan KK Aug 21 '18 at 10:27
  • when you say "I think data is fine" that doesn't mean you debugged anything. You either know or you don't. which one is it? – Andrei Dragotoniu Aug 21 '18 at 10:28
  • 'Replace("i think", "I am sure");' – Shahjahan KK Aug 21 '18 at 10:31
  • @ShahjahanKK might be Issue with "Self referencing loop detected" solution like either you just change ConfigureServices method startup.cs for change AddJsonOptions in AddMvc() like services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); or convert your object into json first then after return as json content. – Lalji Dhameliya Aug 21 '18 at 10:35
  • https://stackoverflow.com/questions/13510204/json-net-self-referencing-loop-detected#45174502 – Lalji Dhameliya Aug 21 '18 at 10:36
  • @LaljiDhameliya yes, that's exactly what i have needed, It's working fine Thank you. – Shahjahan KK Aug 21 '18 at 10:44

1 Answers1

0

Problem in above code: is Json .net self referencing loop detection and the solution is that, we have to ignore loop references and not to serialize them For doing this in Asp.net MVC 2.0 we have to put this code in Startup.cs.

services.AddMvc().AddJsonOptions(
                options => options.SerializerSettings.ReferenceLoopHandling =
                Newtonsoft.Json.ReferenceLoopHandling.Ignore);

Rebuild, problem solved.

Shahjahan KK
  • 91
  • 1
  • 3
  • 12
  • 1
    A better option would be to create a viewmodel and populate it with the data you want and do not expose your domain model to the outside world. – Fran Aug 21 '18 at 11:17
  • in that case do we still be needing to ignore loop reference? – Shahjahan KK Aug 21 '18 at 11:28
  • 1
    It depends on the shape of the data, but you could flatten the data or make the relationship one way which would remove the circular reference. – Fran Aug 21 '18 at 11:31