.net core : incomplete JSON response

后端 未结 6 1864
萌比男神i
萌比男神i 2020-12-18 21:08

I\'m trying to build simple API for training, in my database I got users (firstname, lastname, email password, list) and sports ( name, userID). A

6条回答
  •  独厮守ぢ
    2020-12-18 21:12

    Just to add another yet unique scenario where this can occur. This can also happen if your DAL is returning queryables. In my scenario, I was returning a boxed object from the DAL and had something like this as a linq query

     ...
     RootLevelProp1 = "asd",
     RootLevelProp2 = "asd",
     Trades = b.Trades.OrderBy(c => c.Time).Select(c => new
                    {
                        c.Direction,
                        c.Price,
                        c.ShareCount,
                        c.Time
                    }) //<---- This was being returned as a queryable to the controller 
    

    The Trades query was never being executed even though it's root object had .ToListAsync() called on it. What was happening was that the controller would return the result but only up to the Trades section and the Json would not be terminated properly. I then realized an exception was being caught in some custom middleware I wrote in which it was complaining about the data reader already being open. Without going to deep into my investigation, I assumed it had to do something with the DI and how it was handling the lifecycle of the context. The fix was to just add ToList on the trades. It's an ugly way to pass data from the DAL but this is just a fun project.

提交回复
热议问题