WCF Web 服务:响应为 200/ok,但响应正文为空
我正在创建 WCF Web api 服务。我的问题是某些方法返回 200/OK 响应,但标头和正文为空。
在设置 Web 服务时,我创建了一个 ADO.NET 实体数据模型。我在添加代码生成项时选择了ADO.NET DbContext Generator。在 Model.tt 文档中,我将 HashSet 和 ICollection 更改为 List。我建立了我的网站。
过去,当我编写一个方法来返回实体列表(例如 Northwind 数据库中的 List
或 List
)时,它会起作用美好的。随着时间的推移,我无法返回其中任何一个的列表,而只能获取一个实体。现在,我可以返回 List
或 List
,但不能返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论