Search code examples
entity-frameworkwcfasp.net-mvc-4ef-database-first

An existing connection was forcibly closed by the remote host. in WCF using database first approach


I'm trying to display data in a table format, by joining two tables 'Users' and 'Company' with Foreign key relation.

When I directly writes the code in controller with working fine

public ActionResult Index() {

    var model = new UserModel();

   model.Users = db.Users
        .OrderBy(o => o.CreatedBy)
        .Include(c => c.Company).ToList();

    return View(model);

}

but when i place the same code in WCF is throwing exception..

public List GetUsersList() {

    try
    {
        using (ProductionEntities db = new ProductionEntities())
        {

            db.Configuration.LazyLoadingEnabled = false;
            db.Configuration.ProxyCreationEnabled = false;
            IQueryable<User> _users = db.Users
                .OrderBy(o => o.CreatedBy)
                .Include(c => c.Company);

            return _users.ToList();
        }
    }
    catch (Exception ex)
    {
        ExceptionData exceptionData = new ExceptionData();

        exceptionData.ErrorMessage = "Error in GetUsersList";
        exceptionData.ErrorDetails = ex.ToString();

        throw new FaultException<ExceptionData>(exceptionData, ex.Message);
    }
} 

The below is the Stack Trace Inner exception :

InnerException: System.Net.WebException
       HResult=-2146233079
       Message=The underlying connection was closed: An unexpected error occurred on a receive.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       InnerException: System.IO.IOException
            HResult=-2146232800
            Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
            Source=System
            StackTrace:
                 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                 at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                 at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
            InnerException: System.Net.Sockets.SocketException
                 HResult=-2147467259
                 Message=An existing connection was forcibly closed by the remote host
                 Source=System
                 ErrorCode=10054
                 NativeErrorCode=10054
                 StackTrace:
                      at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                      at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

Any idea??? Thank you,


Solution

  • That's due to the circular reference.

    Check this link for Reference

    also we need to set

    ProductionEntities db = new ProductionEntities()
    db.Configuration.LazyLoadingEnabled = false;
    db.Configuration.ProxyCreationEnabled = false;