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

led*_*per 7 c# wcf entity-framework ef-code-first

在你投票之前,我知道有一些重复的问题,比如这个问题,但是我尝试过但没有成功找到解决方案.可能是我错过了一些东西,但我对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<ServiceFault>(fault, fault.Message, new FaultCode(fault.Reason), fault.Operation);
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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<DateTime> LastModifiedDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

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

<?xml version="1.0"?>
    <configuration>
      <connectionStrings>
        <add name="NotMyDBContextDB" connectionString="[omitted]" providerName="System.Data.SqlClient"/>
        <add name="ApplicationContext" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
      </connectionStrings>
   <system.web>
     <compilation debug="true" targetFramework="4.0"/>
   </system.web>
   <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
          <behavior>
             <serviceMetadata httpGetEnabled="true"/>
             <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

有人能指出我在哪里错了吗?

这是我得到的确切错误:

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
Run Code Online (Sandbox Code Playgroud)

led*_*per 10

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

现在它有效!

至于为什么这会导致一个问题,我的假设是序列化或DbContext问题,但我不得不更多地考虑它,不确定.

我现在出去了.

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

Check to see if your object has virtual keyword on it.
Run Code Online (Sandbox Code Playgroud)


The*_*Man 7

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

WCF服务和对象构造函数