WCF Web 服务:响应为 200/ok,但响应正文为空

发布于 2024-12-12 22:41:05 字数 1316 浏览 1 评论 0原文

我正在创建 WCF Web api 服务。我的问题是某些方法返回 200/OK 响应,但标头和正文为空。

在设置 Web 服务时,我创建了一个 ADO.NET 实体数据模型。我在添加代码生成项时选择了ADO.NET DbContext Generator。在 Model.tt 文档中,我将 HashSet 和 ICollection 更改为 List。我建立了我的网站。

过去,当我编写一个方法来返回实体列表(例如 Northwind 数据库中的 ListList)时,它会起作用美好的。随着时间的推移,我无法返回其中任何一个的列表,而只能获取一个实体。现在,我可以返回 ListList,但不能返回 List 或任何实体的实例。当我尝试获取 List 时,响应为 200/OK,但响应标头和正文为空。

我尝试过使用调试器和 Firefox 的 Web 控制台。使用FF的WC,我只能得到“未定义”的状态代码。我不知道从这里该去哪里。

编辑:在尝试从数据库中获取所有区域时,我这样做:

[WebGet(UriTemplate = "areas")]
public List<a1Areas> AllAreas()
{
    return context.a1Areas.ToList();
}

我将不胜感激任何更多的调试方法。提前致谢。

找到答案了,感谢梅林!

在我的 Global.asax 文件中,我忘记注释掉处理代理和处理上下文对象的两行。代码如下:

void Application_BeginRequest(object sender, EventArgs e)
{
  var context = new AssignmentEntities();
  context.Configuration.ProxyCreationEnabled = false;
  HttpContext.Current.Items["_context"] = context;
}

void Application_EndRequest(object sender, EventArgs e)
{
  var context = HttpContext.Current.Items["_context"] as AssignmentEntities;
  if (context != null)
  {
    context.Dispose();
  }
}

I am creating a WCF web api service. My problem is that some methods return a 200/OK response, but the headers and the body are empty.

In setting up my web service, I created an ADO.NET Entity Data Model. I chose ADO.NET DbContext Generator when I added a code generation item. In the Model.tt document, I changed HashSet and ICollection to List. I built my website.

It used to be that when I coded a method to return a List of an entity (like List<Customer> or List<Employee> in the Northwind database), it worked fine. Over time, I could not return a List of any of those, and could only grab one entity. Now, it's gotten to a point where I can return a List<string> or List<int>, but not a List or an instance of any entity. When I try to get a List<AnyEntity>, the response is 200/OK, but the response headers and body are empty.

I have tried using the debugger and Firefox's Web Console. Using FF's WC, I could only get an "undefined" status code. I am not sure where to go from here.

EDIT: In trying to grab all Areas from the database, I do this:

[WebGet(UriTemplate = "areas")]
public List<a1Areas> AllAreas()
{
    return context.a1Areas.ToList();
}

I would appreciate any more methods for debugging this. Thanks in advance.

Found the answer, thanks to Merlyn!

In my Global.asax file, I forgot to comment out two lines that took care of proxies and disposing of my context object. The code is below:

void Application_BeginRequest(object sender, EventArgs e)
{
  var context = new AssignmentEntities();
  context.Configuration.ProxyCreationEnabled = false;
  HttpContext.Current.Items["_context"] = context;
}

void Application_EndRequest(object sender, EventArgs e)
{
  var context = HttpContext.Current.Items["_context"] as AssignmentEntities;
  if (context != null)
  {
    context.Dispose();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文