接收 HTTP 响应时发生 WCF 服务错误

Posted

技术标签:

【中文标题】接收 HTTP 响应时发生 WCF 服务错误【英文标题】:WCF Service error occurred while receiving HTTP response 【发布时间】:2020-07-24 02:28:07 【问题描述】:

由于服务端点绑定未使用 HTTP 协议,在接收 HTTP 响应时发生错误。这也可能是由于服务器中止了 HTTP 请求上下文。无法从传输连接读取数据。现有连接被远程主机强行关闭

WEB客户端配置

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="basicHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                </security>
            </binding>
        </basicHttpBinding>         
    </bindings>
    <endpoint address="http://WCFService:8086/WCFBase" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" contract="WCFBase.IWCFBase" name="basicHttpBinding"/>
    <endpoint address="http://WCFService:8087/WCFExtra" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" contract="WCFExtra.IWCFEXTRA" name="basicHttpBinding"/>
</system.serviceModel>

WCF 服务配置

<system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding" transferMode="Streamed" />        
      </basicHttpBinding>     
    </bindings>
    <services>      
      <service behaviorConfiguration="WCFService.Web.Behavior" name="WCFService.Web.Service.WCFBase">
        <endpoint address="" binding="basicHttpBinding" name="basicHttpBinding" contract="WCFService.Web.Service.IWCFBase" behaviorConfiguration="WsdlSampleEndpointBehavior" />
        <host>
          <baseAddresses>
            <add baseAddress="http://WCFService:8086/WCFBase" />
          </baseAddresses>
        </host>
      </service>
      <service behaviorConfiguration="WCFService.Web.Behavior" name="WCFService.Web.Extra.WCFExtra">
        <endpoint address="" binding="basicHttpBinding" name="basicHttpBinding" contract="WCFService.Web.Extra.IWCFExtra" behaviorConfiguration="WsdlSampleEndpointBehavior" />
        <host>
          <baseAddresses>
            <add baseAddress="http://WCFService:8087/WCFExtra" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WsdlSampleEndpointBehavior">
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WCFService.Web.Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceThrottling maxConcurrentCalls="3000"     
              maxConcurrentSessions="3000" maxConcurrentInstances="3000"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

网页客户端出错

http://webclient/Common/Login.aspx|System.ServiceModel.CommunicationException:接收到http://wcfservice:8086/WCFBase 的 HTTP 响应时出错。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。有关更多详细信息,请参阅服务器日志。 ---> System.Net.WebException:底层连接已关闭:接收时发生意外错误。 ---> System.IO.IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。 ---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭 在 System.Net.Sockets.Socket.Receive(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags) 在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.PooledStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.Connection.SyncRead(HttpWebRequest 请求,布尔 userRetrievedStream,布尔 probeRead) --- 内部异常堆栈跟踪结束 --- 在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度超时) --- 内部异常堆栈跟踪结束 ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory.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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   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 WCFBase.IWCFBase.PerformUserLogin(UserLoginVO aUserLoginVO)
   at WCFBase.WCFBaseClient.PerformUserLogin(UserLoginVO aUserLoginVO) in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\WebClient\ec042b76\af1d169c\App_WebReferences.39m1idzl.0.cs:line 36589
   at Web.Client.Common_Login.pLogin(Boolean aIsFroceLogin) in d:\webclient\Common\Login.aspx.cs:line 178

【问题讨论】:

【参考方案1】:

默认情况下,WCF 项目不支持多重服务合同。您如何托管具有多个服务合同的 WCF 服务? 客户端配置有问题。客户端配置是用Adding Service Reference工具自动生成的吗? 致电前请确保服务正常运行。 如果问题仍然存在,请随时告诉我。

【讨论】:

以上是关于接收 HTTP 响应时发生 WCF 服务错误的主要内容,如果未能解决你的问题,请参考以下文章

wcf HTTP 终止

学会WCF之试错法——客户端调用基础

WCF 基础连接已经关闭: 服务器关闭了本应保持活动状态的连接。

这可能是由于服务端点绑定未使用 HTTP 协议

WCF - 接收到 http://xxxxx/Service/ 的 HTTP 响应时出错

C# WCF的通信模式