gpt4 book ai didi

c# - ApiController 返回 504 - 服务器未返回此请求的完整响应。服务器返回 0 字节

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:03 24 4
gpt4 key购买 nike

我调用了我的 ApiController,它返回了 504,但我无法弄清楚我做错了什么......

谁能看到我的问题?

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

这是由 Entity Framework Database First 生成的类 SA_Locations...

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,但我在 Fiddler 中看到的响应是:

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

对象是否太复杂而无法返回?

非常感谢任何帮助!

崔维

更新

我决定尝试看看如果我只从 Controller 返回纯 JSON 会发生什么,所以我按如下方式重写了 Controller :

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 相关的事情,正如我所说,SA_Locations 是 Entity Framework 数据库首先生成的类....

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

最佳答案

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

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

HTH

关于c# - ApiController 返回 504 - 服务器未返回此请求的完整响应。服务器返回 0 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38718366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com