WCF – 接收到http:// xxxxx / Service /的HTTP响应时发生错误

在你投票之前,我知道有一些重复的问题,比如这个问题,但是我试过并且没有成功找到它们的解决方案。 可能是我错过了一些东西,但我对WCF还是比较新的,所以有些东西让我头脑发热。

我在我的WCF服务中进行此调用:

public User GetStudentRecord(string userName) { try { return new DashboardFacade().GetStudentRecord(userName); } catch (Exception ex) { ServiceFault fault = new ServiceFault(); fault.Operation = "GetStudentRecord"; fault.Reason = "Who knows..."; fault.Message = ex.Message; throw new FaultException(fault, fault.Message, new FaultCode(fault.Reason), fault.Operation); } } 

DashboardFacade()。GetStudentRecord(..)的定义如下:

 public User GetStudentRecord(string userName) { User user = ctx.Users.Where(x => x.ADUserName == userName).SingleOrDefault(); return user; } 

ctx是我的DbContext。 我正在使用Entity Framework Code First。 User对象如下所示:

 public class User { public User() { //UserDetails = new UserDetail(); } [Key] public Guid MasterKey { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string ADUserName { get; set; } public string UserType { get; set; } public virtual UserDetail UserDetails { get; set; } public Nullable LastModifiedDate { get; set; } } 

最后,您可能需要在WCF应用程序上看到web.config:

                          

谁能指出我在哪里错了?

这是我得到的确切错误:

 An error occurred while receiving the HTTP response to http://localhost:5843/MyService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. Inner Exception: The underlying connection was closed: An unexpected error occurred on a receive. Inner Exception: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. Inner Exception: An existing connection was forcibly closed by the remote host 

在经过大量宣誓之后找到了根本原因,并想到外面的天气有多好。 我从User对象内的UserDetails对象中删除virtual关键字。

现在它有效!

至于为什么这会引起一个问题,我的假设是序列化或DbContext问题,但我必须更多地考虑它,不确定。

我现在出去了。

所以作为参考,如果你在这里结束并且不知道发生了什么,在你应该看的所有其他事情中(大小,超时等):

 Check to see if your object has virtual keyword on it. 

我遇到了这个问题,在我的情况下,问题是WCF服务返回的是一个只有getter而没有setter的属性的类。 我试图阻止接收器修改属性。 要解决这个问题,请看…

WCF服务和对象构造函数

我有同样的错误。

在我的例子中,我有一个名为OEM的int列的表。 在模型层中,我有一个类(DTO),该列由Enum表示。 表中有一行,OEM列中的值无效。 当我尝试使用LINQ提供所有数据时,VisualStudio未捕获到错误。 当WCF尝试检索消息时引发该错误。

当您尝试访问返回的User对象的某个字段时,是否会发生exception?

如果是这样,在调用GetStudentRecord()之后,您的服务似乎正在关闭您的ctx对象。 你在哪里创建ctx以及如何打开/关闭它?

(由于50个代表的要求,我不能把它放在评论中…所以它在这里作为答案而不是)

这些方法都不适合我。 我所做的是在从WCF测试客户端运行时调试服务,我能够找到我的问题。 这与服务库中缺少app.config中的连接字符串有关。

如果有人尝试了所有其他方法但仍然无法找到问题,请发布。