9

我正在使用 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>
4

4 回答 4

10

它的常见(通用)消息。

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

于 2013-04-07T04:28:59.220 回答
5

我在 WCF 中的 http 绑定的深处也遇到了类似的错误。

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

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

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

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

于 2015-09-10T12:46:58.013 回答
3

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

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

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

于 2016-01-11T18:18:45.547 回答
1

在您的 app.config / web.config 中配置 WCF 跟踪并检查Error.svclog(它将在您的二进制文件附近创建)以获取详细信息。就我而言,这是因为使用了自动属性初始化程序(没有设置器的属性)—— 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>
于 2018-01-18T08:31:57.223 回答