Mvc: Web API doesnt work when getting data from EF Core using .Include

Created on 6 Mar 2017  路  11Comments  路  Source: aspnet/Mvc

Hello guys,
I setted up an API to return data from my database.

Everything went fine in a case like this:

    [HttpGet()]
    public IEnumerable<table1> table1()
    {
        var a = _context.table1.Take(10).ToList();
        var b = _context.table1.Include(i => i.table2).Take(10).ToList();
        return a;
    }

but if I set return to "b" the API stops (when debugging in VS2017, both returns valid data).

checking with Swagger (or Postman) it says: "no content" with a response code 0.

If I try to check the API url direct from browser it shows just 1 record (debugging shows correctly the 10 records).

not sure if I made myself clear, I can provide more data/screenshots if needed.

Thanks

invalid

Most helpful comment

thats it, edited Startup.cs and change:

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

did the trick and now its working (just didnt understood if its an error or working as intended)

thanks

All 11 comments

Can you post your model classes?

Hi, Im sending attached. Note that those models were generated automatically by Scaffold-DbContext.

also I was checking the console.log and noticed that when I use .Include, the response is:

GET http://localhost:50757/api/NotasFiscais/ net::ERR_CONNECTION_RESET
XHR failed loading: GET "http://localhost:50757/api/NotasFiscais/".
EXCEPTION: Response with status: 0 for URL: null

also if I tried direct with curl I get:
PS C:\Users\flavi> curl 'http://localhost:50757/api/NotasFiscais'
curl : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
At line:1 char:1
+ curl 'http://localhost:50757/api/NotasFiscais'

NotasFiscais.txt
Fornecedores.txt

@Cae2 I'm guessing that an exception is being thrown on the server. Have you tried running it with the debugger to see if that's the case?

@Eilon There is no exception on server, it seems that the http connection is suddenly dropping.

I noticed that running the API uri on chrome, for example in the first attempt:
1

and after a F5:
2

in the second attempt the result was more complete (it seems that at least 1 record is complete, but the connection was dropped with "net::ERR_CONNECTION_RESET" before receive another 9 for example)

@cesarbs any idea on what this could be?

@Eilon , Running via powershell finally shows me an error:

3

it seems that is related to:
https://github.com/aspnet/IISIntegration/issues/285

but there is a lot of info there, will need to take a more deeply look, not sure if its still "fixed"

thats it, edited Startup.cs and change:

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

did the trick and now its working (just didnt understood if its an error or working as intended)

thanks

Yeah by default Json.NET will throw if you try to serialize circular references (because it's impossible 馃槃 ).

The reason the connection is reset is that the exception happens while the response has already started writing, and there's no way to un-write data in a response, so this is the only option to end the request.

@Cae2 - Thanks a ton!! Your solution save my day!! :)

@Cae2 Thanks, this worked for me too. :)

I had an _intermittent_ problem with this error, apparently there's a race condition in older versions of the .NET Core Windows Server Hosting bundle where the connection will sometimes/often close normally before terminating this way. The issue was blocking us from upgrading and getting to be extremely annoying for users.

All because I wrote this in front of a property: /* TODO: This breaks serialization */
Instead of this: [JsonIgnore]

Thanks for the help tracking that down.

Was this page helpful?
0 / 5 - 0 ratings