Search code examples
c#asp.net-apicontroller

ApiController returning 504 - The server did not return a complete response for this request. Server returned 0 bytes


I have a call to my ApiController which is returning a 504 and I just cannot work out what I am doing wrong...

Can anyone see my problem?

This is the controller:

public class SA_GetAllLocationsController : ApiController
{
    [Route("SA_GetAllLocations")]
    [HttpGet]
    public List<SA_Locations> Get()
    {
        List<SA_Locations> result = SADatastore.Functions.Location.GetAllLocations(Settings.Default.ConnectionString);

        return result;
    }
}

This is the class SA_Locations which was generated by Entity Framework Database First...

public partial class SA_Locations
{
    public SA_Locations()
    {
        this.SA_Items = new HashSet<SA_Items>();
    }

    public string LocationID { get; set; }
    public string BuildingID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string MISID { get; set; }
    public string GPSCoords { get; set; }
    public string QRCode { get; set; }
    public Nullable<bool> Signable { get; set; }
    public Nullable<bool> Auditable { get; set; }
    public Nullable<bool> Bookable { get; set; }
    public Nullable<bool> Entrance { get; set; }
    public Nullable<bool> Exit { get; set; }
    public Nullable<bool> Active { get; set; }
    public System.DateTime LastUpdated { get; set; }

    public virtual SA_Buildings SA_Buildings { get; set; }
    public virtual ICollection<SA_Items> SA_Items { get; set; }
}

I have written a small "status" function that works perfectly and looks like this:

public HttpResponseMessage Get()
    {
        return new HttpResponseMessage()
        {
            Content = new StringContent(
                "SA service is running and ready to accept connections!",
                Encoding.UTF8,
                "text/html"
            )
        };
    }

I just cannot work out why the SA_Locations appears to be being returned with no errors when I debug but the response I see in Fiddler is:

504 - The server did not return a complete response for this request. Server returned 0 bytes

Is the object too complex to be returned?

Any help would be very much appreciated!

Trev

UPDATE

I decided to try and see what happens if I just return pure JSON from the controller so I re-wrote the controller as follows:

public class SA_GetAllLocationsController : ApiController
{
    [Route("SA_GetAllLocations")]
    [HttpGet]
    public HttpResponseMessage Get()
    {
        List<SA_Locations> result = SADatastore.Functions.Location.GetAllLocations(Settings.Default.ConnectionString);

        string output = JsonConvert.SerializeObject(result);

        return new HttpResponseMessage()
        {
            Content = new StringContent(
                output,
                Encoding.UTF8,
                "application/json"
            )
        };
    }
}

It now actually throws an error serializing the SA_Locations object to a string!

The error message is:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Error getting value from 'SA_Buildings' on 'System.Data.Entity.DynamicProxies.SA_Locations_720FF8784B152496318F87BCB674E344B97C0446B16D4DC2BDDC381D88C048B9'.

So, it does point toward something to do with SA_Locations, which as I said, is a class generated by Entity Framework Database first....

Getting closer but no idea why that object won't serialize?


Solution

  • This all boiled down to Configuration.ProxyCreationEnabled = false;

    https://stackoverflow.com/a/12869077/1153795

    HTH