c# - WCF 错误 "The socket connection has been disposed"

标签 c# .net wcf .net-4.0

我有一个 WCF 服务,可以根据输入参数返回大量数据。

该服务作为 ServiceHost 可执行文件在我的机器上运行,我现在正在使用 WCF 测试客户端进行测试。

这是我的主机配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_ExchangeRepository"
                         closeTimeout="00:01:00" openTimeout="00:01:00"
                         receiveTimeout="00:10:00" sendTimeout="00:10:00"
                         transactionFlow="false" transferMode="Streamed"
                         transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard"
                         listenBacklog="500"    maxBufferPoolSize="2147483646"
                         maxBufferSize="2147483646" maxConnections="500"
                         maxReceivedMessageSize="2147483647"  >
                    <readerQuotas
                        maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                    <security mode="None">
                    </security>
                </binding>          
            </netTcpBinding>
        </bindings>         
        <services>          
            <service name="MyCompany.MyApplication.DataServices.ExchangeRepository" 
                     behaviorConfiguration="MyCompany.MyApplication.DataServicesBehavior">
                <endpoint address ="" 
                          binding="netTcpBinding" 
                          contract="MyCompany.MyApplication.DataServices.IExchangeRepository"
                          bindingConfiguration="NetTcpBinding_ExchangeRepository" 
                          behaviorConfiguration="MyCompany.MyApplication.EndpointBehavior">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                <host>
                    <baseAddresses>
                        <add baseAddress = "net.tcp://localhost:8896/ExchangeRepository/" />
                        <add baseAddress = "http://localhost:8895/ExchangeRepository/" />
                    </baseAddresses>
                </host>
            </service>
        </services>         
        <behaviors>
            <serviceBehaviors>
                <behavior name="MyCompany.MyApplication.DataServicesBehavior">
                    <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
                    <serviceMetadata httpGetEnabled="True"/>
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="MyCompany.MyApplication.EndpointBehavior">
                    <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
                </behavior>
            </endpointBehaviors>
        </behaviors>    
    </system.serviceModel>
</configuration>

这是我的客户端配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="endpointBehaviour">
                    <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IExchangeRepository" closeTimeout="00:10:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="2147483647" maxConnections="10"
                    maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="None">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:8896/ExchangeRepository/"
                behaviorConfiguration="endpointBehaviour" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IExchangeRepository" contract="IExchangeRepository"
                name="NetTcpBinding_IExchangeRepository">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

这是我得到的错误:

The socket connection has been disposed.

Server stack trace: 
   at System.ServiceModel.Channels.SocketConnection.ThrowIfNotOpen()
   at System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at System.ServiceModel.Channels.PreReadConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at System.ServiceModel.Channels.SingletonConnectionReader.SingletonInputConnectionStream.ReadCore(Byte[] buffer, Int32 offset, Int32 count)
   at System.ServiceModel.Channels.SingletonConnectionReader.SingletonInputConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.ServiceModel.Channels.MaxMessageSizeStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.ServiceModel.Channels.SingletonConnectionReader.Close(TimeSpan timeout)
   at System.ServiceModel.Channels.SingletonConnectionReader.SingletonInputConnectionStream.Close()
   at System.ServiceModel.Channels.DelegatingStream.Close()
   at System.Xml.XmlBufferReader.Close()
   at System.Xml.XmlBaseReader.Close()
   at System.Xml.XmlBinaryReader.Close()
   at System.Xml.XmlReader.Dispose(Boolean disposing)
   at System.Xml.XmlReader.Dispose()
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   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 IExchangeRepository.SearchExchangeRequest(String AccountNumber, Int32 StatusCode, Int32 StartDate, Int32 EndDate)
   at ExchangeRepositoryClient.SearchExchangeRequest(String AccountNumber, Int32 StatusCode, Int32 StartDate, Int32 EndDate)

Inner Exception:
The socket connection has been disposed.
Object name: 'System.ServiceModel.Channels.SocketConnection'.

当返回的数据多于一定数量的记录时会发生错误。使用较少的数据,一切都按预期工作。

关于如何解决这个问题有什么建议吗?

最佳答案

这一切都是由愚蠢的数据问题引起的。 错误信息是绝对没有用的。启用跟踪允许查看实际异常。 谢谢你,EkoostikMartin!

关于c# - WCF 错误 "The socket connection has been disposed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162190/

相关文章:

c# - setRequestHeader 未将请求 header 添加到 wcf 调用

web-services - 使用 WCF 使用 HTTPS Web 服务

c# - 使用 ExecuteScalar 从 db 中读取 MAX 值并避免为 null

C# LINQ 创建月份和年份列表

C# 计算重复数字

c# - 如何同时运行 4 个进程?

.net - OracleConnection 在托管 C++ 中抛出 EEMessageException

.net - 对 Atom 提要中的命名空间感到困惑

c# - 如何强制 Unity 创建新实例?

c# - Windows uwp 中的中心命令栏按钮