2014-10-10 104 views
0

我試圖以表格格式顯示數據,通過將外鍵關係連接到兩個表'用戶'和'公司'。現有連接被遠程主機強制關閉。在WCF中使用數據庫第一種方法

當我直接與工作正常

公衆的ActionResult指數() {

var model = new UserModel(); 

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

    return View(model); 

}

,但是當我把在WCF相同的代碼拋出異常寫代碼控制器...

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); 
    } 
} 

下面是堆棧跟蹤內部異常:

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) 

任何想法??? 謝謝

回答

3

這是由於循環引用。

檢查此鏈接爲Reference

我們也需要設置

ProductionEntities db = new ProductionEntities() 
db.Configuration.LazyLoadingEnabled = false; 
db.Configuration.ProxyCreationEnabled = false;  
相關問題