Skip to main content
 首页 » 编程设计

c#之接收 HTTP 响应错误时发生错误

2024年02月20日14findumars

在使用 WCF 客户端进行测试时,我的 wcf 服务出现问题。当我调试时,我看到数据从函数返回,但在响应期间出错。有人可以帮我弄清楚我错过了什么吗?

这是我的服务和配置文件:

public List<PO> GetAllPOs() 
{ 
    var data = new List<PO>(); 
    try 
    { 
        var manager = new EntityReaderManager<PO>(ConfigurationManager.AppSettings("FilemakerDB"]); 
        data = manager.GetEntityList(); 
    } 
    catch (Exception ex) 
    { 
        ILogger logger = new Logger(typeof(FilemakerDataService));  
        logger.Error(ex.Message); 
 
    } 
 
   return data; 
} 

这是配置文件:

<system.serviceModel>     
    <diagnostics> 
      <messageLogging logEntireMessage="true" logKnownPii="true" logMalformedMessages="true" 
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" 
        maxSizeOfMessageToLog="2147483647" /> 
      <endToEndTracing propagateActivity="true" activityTracing="false" 
        messageFlowTracing="true" /> 
    </diagnostics> 
 
    <bindings> 
    <wsHttpBinding> 
      <binding name="Custom.WSHTTPBinding.Configuration" closeTimeout="10:10:00" 
        openTimeout="10:10:00" receiveTimeout="10:10:00" sendTimeout="10:10:00" 
        maxBufferPoolSize="655360" maxReceivedMessageSize="655360"> 
 
        <security mode="None" /> 
      </binding> 
    </wsHttpBinding> 
  </bindings> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" /> 
    <services> 
      <service behaviorConfiguration="Custom.ServiceBehavior" name="AerWorldwide.Service.Web.FilemakerData.FilemakerDataService"> 
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Custom.WSHTTPBinding.Configuration" 
          name="Custom.WSHTTPBinding.Configuration" contract="AerWorldwide.Service.Web.FilemakerData.IFilemakerDataService"> 
          <identity> 
            <dns value="localhost" /> 
          </identity> 
        </endpoint> 
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      </service> 
    </services> 
  <behaviors> 
    <serviceBehaviors> 
      <behavior name="Custom.ServiceBehavior"> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="true" /> 
      </behavior> 
    </serviceBehaviors> 
  </behaviors>     
</system.serviceModel> 

错误:

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)  

请您参考如下方法:

首先启用Tracing在您的服务上查看异常的原因。

您还可以考虑增加服务器端和客户端的 ReaderQuotas,以便毫无问题地传递更大的数据。示例如下:

<wsHttpBinding> 
      <binding name="Custom.WSHTTPBinding.Configuration" closeTimeout="10:10:00" 
        openTimeout="10:10:00" receiveTimeout="10:10:00" sendTimeout="10:10:00" 
        maxBufferPoolSize="655360" maxReceivedMessageSize="655360"> 
         <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
              maxNameTableCharCount="2147483647" /> 
        <security mode="None" /> 
      </binding> 
    </wsHttpBinding> 

我还在您的代码中看到您正在直接传递由 Entity Framework 获取的对象。在某些情况下, Entity Framework 对象不会被反序列化并可能导致异常。创建一个简单的 POCO,然后填充获取的数据并返回 POCO。