WCF - An error occurred while receiving the HTTP response to http://xxxxx/Service/

c# ef-code-first entity-framework wcf


I am making this call in my WCF service:

public User GetStudentRecord(string userName)
            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);

DashboardFacade().GetStudentRecord(..) is defined like this:

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

ctx is my DbContext. I am using Entity Framework Code First. The User object looks like this:

public class User
    public User()
        //UserDetails = new UserDetail();
    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; }

And finally, you probably need to see the web.config on the WCF Application:

<?xml version="1.0"?>
        <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"/>
     <compilation debug="true" targetFramework="4.0"/>
             <serviceMetadata httpGetEnabled="true"/>
             <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>

Can anyone point me to where I am going wrong here?

This is the exact error I am getting:

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
6/12/2019 4:35:40 PM

Accepted Answer

Found the root cause after a lot of swearing and thinking of how nice the weather is outside. I remove the virtual keyword from the UserDetails object that is inside the User object.

Now it works!

As far as why this caused an issue, my assumptions are serialization or DbContext issues, but I will have to look more into it, not sure.

I'm going outside now.

So for reference, if you wound up here and have no idea what is going on, among all the other things you should look at (size, timeout, etc):

Check to see if your object has virtual keyword on it.
4/4/2013 10:21:27 PM

Popular Answer

I had this problem and in my case, the issue was the WCF service was returning a class that had a property with only a getter and no setter. I was trying to prevent the property from being modified by the receiver. To get around this, see this...

WCF Services and Object Constructors

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow