【问题标题】:WCF The underlying connection was closed: An unexpected error occurred on a receiveWCF 底层连接已关闭:接收时发生意外错误
【发布时间】:2013-04-07 04:13:36
【问题描述】:

我正在使用 RestClient 应用程序与我的 WCF 服务通信。我收到以下异常

The underlying connection was closed: An unexpected error occurred on a receive.

这是我使用的 C# 代码

            string q = string.Format(@"xxxxxxxxxxxxxxxxxxxxxxxxxxx");
            WebClient client = new WebClient();
            string Url = string.Format("{0}/Get?queries={1}", BaseUrl,HttpUtility.UrlEncodeUnicode(q));
            client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            var result = client.DownloadString(Url);
            Console.WriteLine("======================output================================");
            Console.WriteLine(result);
            Console.WriteLine("===========================================");

这是错误信息

System.Net.WebException was caught
  HResult=-2146233079
  Message=The underlying connection was closed: An unexpected error occurred on a receive.
  Source=System
  StackTrace:
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
       at RestClient.Program.GetRecordsTest() in C:\Users\Wiemon\Downloads\RestAppClient\RestAppClient\Program.cs:line 118
  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.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
            at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
            at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
            at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
            at System.Net.Security._SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)
            at System.Net.TlsStream.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)
            InnerException: 

这是我的绑定

<binding name="wsHttpEndpoint" >
                    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    <security mode="Transport">
                        <transport clientCredentialType="Certificate" />
                        <message clientCredentialType="Certificate" />
                    </security>
                </binding>

还有我的端点行为,而不是我设置 maxItemsInObjectGraph="2147483647"

<behavior name="MyEndpointBehavior">
                    <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                    <clientCredentials>
                        <clientCertificate findValue="xxxxxxxxxxxxxxxxxx" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" />
                        <serviceCertificate>
                            <authentication certificateValidationMode="None" revocationMode="NoCheck" />
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>

【问题讨论】:

    标签: c# wcf wcf-rest


    【解决方案1】:

    它的常见(通用)消息。

    您应该使用Trace Viewer Tool (SvcTraceViewer.exe) 来找出服务器错误

    【讨论】:

    • 谢谢。正如您所建议的,我启用了跟踪,然后使用 Traceviewer 工具扫描了日志。这有助于我识别问题。
    【解决方案2】:

    由于我在 WCF 中的 http 绑定的深度,我遇到了类似的错误。

    客户端 -> 底层连接已关闭:接收时发生意外错误。

    客户端 -> 无法从传输连接读取数据:现有连接被远程主机强行关闭。

    服务器(在“procdump”的帮助下得到这个)-> I/O 操作由于线程退出或应用程序请求而中止

    最终在几个小时后,我发现了 HTTP.sys 日志记录(在 C:\WINDOWS\System32\LogFiles\HTTPERR 中),我发现 WCF 自托管服务连接被强制删除(故意),因为模糊的配置问题(每秒最小发送速率字节)。不幸的是,重新配置它又花了几个小时(你不能通过“netsh http add timeout”来做到这一点,所以你必须在应用程序内或非自托管时在 IIS 内进行)。

    【讨论】:

    • 您能详细说明解决方案吗?您有任何可能有用的参考资料吗?
    • 我有一个与 Microsoft 合作的支持案例,他们说 Windows 2012 R2 中可能存在与 HTTP.sys 超时(每秒最小发送速率字节数)之一相关的重大变化。它主要在慢速 WAN 连接上体验。我将此讨论移至 social.msdn.microsoft.com(搜索“MinSendBytesPerSecond 上的 WCF 错误”)
    【解决方案3】:

    我不知道是否是同一个错误,但在我的情况下,异常在 Json 中,无法转换 Date 值,因为它是空的。

    转换为 UTC 时大于 DateTime.MaxValue 或小于 DateTime.MinValue 的 DateTime 值无法序列化为 JSON。

    跟踪查看器对于查找问题非常有用。

    【讨论】:

      【解决方案4】:

      在您的 app.config / web.config 中配置 WCF 跟踪并检查 Error.svclog(它将在您的二进制文件附近创建)以获取详细信息。 就我而言,这是因为使用了自动属性初始化程序(没有设置器的属性) - feature from C# 6.0

      <Message>No set method for property 'FailedCount' in type 'MyProject.Contracts.Data.SyncStatusByVersion'.</Message>
      <StackTrace>   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
                     at WriteSyncStatusByVersionToJson(XmlWriterDelegator , Object , XmlObjectSerializerWriteContextComplexJson , ClassDataContract , XmlDictionaryString[] )
                     ...
      </StackTrace>
      

      配置 WCF 跟踪:

      <system.diagnostics>
          <sources>
            <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true" >
              <listeners>
                <add name="xml"/>
              </listeners>
            </source>
            <source name="System.ServiceModel.MessageLogging">
              <listeners>
                <add name="xml"/>
              </listeners>
            </source>
            <source name="myUserTraceSource" switchValue="Information, ActivityTracing">
              <listeners>
                <add name="xml"/>
              </listeners>
            </source>
          </sources>
          <sharedListeners>
            <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="Error.svclog" />
          </sharedListeners>
      </system.diagnostics>
      

      【讨论】:

        猜你喜欢
        • 2011-01-25
        • 2014-03-10
        相关资源
        最近更新 更多