2017-02-27 21 views
0

我正在创建将在数据库中的任何更新数据或从数据库中提取数据一个WCF Web服务时,WCF Webservice的错误。现在我正在处理这个web服务的拉部分。当我尝试将更大的数据集返回给客户端时,我遇到了一个问题。每当我试图与WCF测试客户端,我得到以下错误测试:返回更大的数据集客户端

An error occurred while receiving the HTTP response to http://localhost:56196/CustomerService.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. 

Server stack trace: 
    at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) 
    at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 
    at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) 
    at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) 
    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) 
    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) 
    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) 

Exception rethrown at [0]: 
    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 
    at ISylectusCustomerService.getCustomerData(Int32 pabcode, Int16 CompanyCode, String UserName, String Password) 
    at SylectusCustomerServiceClient.getCustomerData(Int32 pabcode, Int16 CompanyCode, String UserName, String Password) 

Inner Exception: 
The underlying connection was closed: An unexpected error occurred on a receive. 
    at System.Net.HttpWebRequest.GetResponse() 
    at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 

Inner Exception: 
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 
    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) 

Inner Exception: 
An existing connection was forcibly closed by the remote host 
    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) 

下面是我如何我的接口和类建立了一个例子:

namespace SylectusCustomerWebSrv 
{  
    [DataContract] 
    public class Customer 
    { 
     [DataMember] 
     public bool customerExists { get; set; } 
     [DataMember] 
     public string ErrorText { get; set; } 
     [DataMember] 
     public Cust customer; 
     [DataMember] 
     public List<CustContact> custContacts; 

     public Customer() 
     { 
      customerExists = false; 
      ErrorText = ""; 
     } 
    } 

    [DataContract] 
    public class Cust 
    { 
     #region ClassProperties 

     [DataMember] 
     public string CustomerName{ get; set; } 
     //I have a lot more properties here but didnt want to list them all. 

     #endregion 

     public Cust() 
     { 

     } 
    } 

    [DataContract] 
    public class CustContact 
    { 
     #region ClassProperties 

     [DataMember] 
     public int ContactName{ get; set; } 
     //I have a lot more properties here but didnt want to list them all. 
     #endregion 

     public CustContact() { } 

    } 

    [ServiceContract] 
    [XmlSerializerFormat] 
    //note: I'm using xmlserializerformat here so I dont get the specified parameters in my client when I try to call my functions 
    public interface ISylectusCustomerService 
    { 
     [OperationContract] 
     Customer getCustomerName(int pabcode, short CompanyCode, string UserName, string Password); 

     [OperationContract] 
     Customer getCustomerData(int pabcode, short CompanyCode, string UserName, string Password); 
    } 

    public class SylectusCustomerService : ISylectusCustomerService 
    { 
     private DatabaseConnection db; 

     private bool UserAllowed(short companyCode, string username, string password) 
     { 
      bool accessGranted = false; 

      try 
      { 
       //here I check the creds against the database and set accessgranted to either true or false.     
      } 
      catch (Exception ex) 
      { 

      } 
      return accessGranted; 
     } 

     public Customer getCustomerName(int pabcode, short CompanyCode, string UserName, string Password) 
     { 
      Customer cust = new Customer(); 
      cust.customer = new Cust(); 

      cust.ErrorText = ""; 

      if (!UserAllowed(CompanyCode, UserName, Password)) 
      { 
       cust.ErrorText = "Invalid Credentials"; 
       return cust; 
      } 
      else 
      { 
       try 
       { 
        db = DatabaseConnectionFactory.Instance.OpenDatabaseConnection(CompanyCode); 

        if (db.IsOpen) 
        { 
         custController = new custController (db); 
         cust.customer.CustomerName = custController .GetCustomerName(pabcode, CompanyCode.ToString()); 
         db.Close(); 
         if (cust.customer.CustomerName.Length == 0) 
         { 
          cust.ErrorText = "No Customer found for PABCode: " + pabcode; 
         }       
        } 
        else 
        { 
         cust.ErrorText = "Unable to open database"; 
        }      
       } 
       catch (Exception ex) 
       { 
       cust.ErrorText = "Exception: " + ex.Message; 
       } 
       return cust; 
      } 
     } 

     public Customer getCustomerData(int pabcode, short CompanyCode, string UserName, string Password) 
     { 
      Customer cust = new Customer(); 

      cust.ErrorText = ""; 

      if (!UserAllowed(CompanyCode, UserName, Password)) 
      { 
       cust.ErrorText = "Invalid Credentials"; 
      } 
      else 
      { 
       try 
       { 
        string existingCust = custExists(pabcode, CompanyCode); 

        if (existingCust == "true") 
        { 
         db = DatabaseConnectionFactory.Instance.OpenDatabaseConnection(CompanyCode); 

         if (db.IsOpen) 
         { 
          cust.customer = new Cust(); 
          cust.custContacts = new List<CustContact>(); 

          //here I do a sql call and grab the customer data and store it in the cust object 
          SelectStatement sql = new SelectStatement(); 
          sql.Table(custtable); 
          sql.Column(custName); 
          sql.Where(wherestatement); 
          using (QueryResult res = db.Query(sql)) 
          { 
           if (res.Read()) 
           { 
            cust.customer.CustomerName = res.GetString("custName"); 
           } 
          } 
          //here I do a sql call and grab the customer contact data and store it in the custContact list **here is where the issue comes up I think. 
          SelectStatement sql = new SelectStatement(); 
          sql.Table(custContacttable); 
          sql.Column(custContactName); 
          sql.Where(wherestatement); 
          using (QueryResult res = db.Query(sql)) 
          { 
           if (res.Read()) 
           { 
            CustContact cc = new CustContact(); 
            pc.ContactName = res.GetString("custContactName"); 
            cust.custContacts.Add(cc); 
            //if I comment out the previous line then I no longer get the error and the cust.customer object is populated correctly and gets sent to the client. 
           } 
          } 

          db.Close(); 
         } 
         else if (existingCust == "false") 
         { 
          cust.customerExists = false; 
          cust.ErrorText = "No customer exists with pabcode: " + pabcode; 
         } 
         else 
         { 
          cust.customerExists = false; 
          cust.ErrorText = existingCust; 
         } 
        } 
        else 
        { 
         cust.ErrorText = "Unable to open database"; 
        } 
       } 
       catch (Exception ex) 
       { 
        cust.ErrorText = "Exception: " + ex.Message; 
       } 
      } 

      return cust; 
     } 

     public string custExists(int pabcode, short MABCode) 
     { 
      string custExists = "false"; 

      try 
      { 
       //function checks database to see if customer exists 
      } 
      catch (Exception ex) 
      { 
       custExists = "Exception: " + ex.Message; 
      } 

      return custExists; 
     }   
    } 
} 

事情我不是完全理解与此错误是,当我运行getCutomerName功能我没有错误,我可以看到在我的客户类的WCF测试客户端的所有属性生成的XML,这只是发生在我尝试调用getCustomerData,我想知道如果它与我在Cust类中填充更多属性有关?

任何帮助,非常感谢!

更新:我注意到,如果我删除填充custContacts列表中的部分,然后我不再得到一个错误。我意识到我几乎省略了实际填充列表的部分,所以我会在此更新我的代码,以便每个人都可以看到我在做什么。我也尝试使custContact列表只是一个字符串列表,但仍然抛出错误。

+0

把睡眠和尝试调试:它可能是一个超时或检索数据时出错 – 2017-02-27 16:54:22

回答

1

,我发现我的代码的问题,我曾与我是用抢custContacts的SQL错误,我其实是抓住了很多更多的记录比我预想的,所以我认为这是使对象有些不合理的大小。我修复了SQL,现在我没有任何问题。

0

在您的WCF的结合(在web.config中)的配置,你需要添加maxReceivedMessageSize,或者如果它的存在,将其设置为一个更大的尺寸。

How to increase maxReceivedMessageSize

http://craigrandall.net/archives/2009/04/maxreceivedmessagesize/

+0

我增加了maxReceivedMessageSize到2GB,我仍然得到相同的错误 – Kristen

+0

你设置了wcf跟踪,并观察那里发生了什么 - 它可能会给你一个线索,看看出了什么问题。 –

+0

我也这么做了,只有我得到的新错误是“无法分配268435456字节的托管内存缓冲区,可用内存量可能很低。”随后便出现时,它试图通过HTTP发送响应消息 – Kristen

相关问题