ApiController returning 504 - 服务器没有 return 对此请求的完整响应。服务器 returned 0 字节

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

我调用了我的 ApiController,它是 return504,但我无法弄清楚我做错了什么...

谁能看到我的问题?

这是控制器:

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;
    }
}

这是 class SA_Locations 由 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; }
}

我写了一个小的“状态”函数,它运行得很好,看起来像这样:

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

我无法弄清楚为什么 SA_Locations 在我调试时似乎 return 没有错误,但我在 Fiddler 中看到的响应是:

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

对象是否太复杂而无法 return编辑?

非常感谢任何帮助!

特雷夫

更新

我决定尝试看看如果我只是从控制器 return 纯 JSON 会发生什么,所以我按如下方式重写了控制器:

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"
            )
        };
    }
}

它现在实际上在将 SA_Locations 对象序列化为字符串时抛出错误!

错误信息是:

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'.

所以,它确实指向与 SA_Locations 有关的东西,正如我所说,它是由 Entity Framework 数据库首先生成的 class....

越来越近了,但不知道为什么那个对象不会序列化?

这一切归结为 Configuration.ProxyCreationEnabled = false;

HTH