我知道这个问题已经存在很多问题,但没有回复解决了我的问题。
我在运行的服务器上有一个NetTcpBinding,并且不时(不总是,像20到60个调用中的1个,完全随机)我在客户端收到此异常:
套接字连接已中止。这可能是由错误引起的 处理您的消息或超过接收超时 远程主机或底层网络资源问题。本地套接字 超时是'00:01:00'。
我注意到当客户端的连接速度很慢时,这种异常会更频繁地发生。
此外,所述超时时间为1分钟,但例外情况已在1秒左右后发生。
我的所作所为:
var client = GetClient();
client.Open();
bool success = client.Call(); // Exception occurs here (the return value is a bool)
我有时也会在服务器端获得此异常:
TCP错误(995:I / O操作因中断而中止 发送时发生线程退出或应用程序请求) 数据
我启用了服务器跟踪,但是我得到了相同的例外情况,并没有为我提供任何额外的见解。
我的GetClient:
NetTcpBinding netTcpBinding = new NetTcpBinding
{
CloseTimeout = new TimeSpan(0, 0, 30),
OpenTimeout = new TimeSpan(0, 0, 30),
TransferMode = TransferMode.Buffered,
ListenBacklog = 2,
MaxConnections = 2,
MaxReceivedMessageSize = 10485600,
ReaderQuotas = { MaxStringContentLength = 1048576 },
ReliableSession = { Enabled = false },
Security =
{
Mode = SecurityMode.Transport,
Transport = { ClientCredentialType = TcpClientCredentialType.None, ProtectionLevel = ProtectionLevel.EncryptAndSign },
Message = { ClientCredentialType = MessageCredentialType.Windows }
}
};
来自服务的重要配置:
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" httpsHelpPageEnabled="true" />
<serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="My.Service.WCF.Tools.CustomAuthorizationManager, My.Service.WCF">
<authorizationPolicies>
<clear/>
<add policyType="My.Service.WCF.Tools.CustomAuthorizationPolicy, My.Service.WCF" />
</authorizationPolicies>
</serviceAuthorization>
<serviceCredentials>
<serviceCertificate findValue="MyService" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
</serviceCredentials>
<serviceThrottling maxConcurrentCalls="65536" maxConcurrentSessions="65536" maxConcurrentInstances="65536" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="ServiceBinding" receiveTimeout="00:05:00" maxConnections="65536" listenBacklog="65536">
<transactionFlow />
<sslStreamSecurity requireClientCertificate="false" />
<binaryMessageEncoding />
<tcpTransport transferMode="Buffered" portSharingEnabled="true" maxReceivedMessageSize="1048576" maxPendingConnections="65536" maxPendingAccepts="10">
<connectionPoolSettings leaseTimeout="00:05:00" idleTimeout="00:05:00" maxOutboundConnectionsPerEndpoint="65536" />
</tcpTransport>
</binding>
<binding name="MexBinding" receiveTimeout="00:05:00" maxConnections="65536" listenBacklog="65536">
<tcpTransport transferMode="Buffered" portSharingEnabled="true" maxReceivedMessageSize="1048576" maxPendingConnections="65536" maxPendingAccepts="10">
<connectionPoolSettings leaseTimeout="00:05:00" idleTimeout="00:05:00" maxOutboundConnectionsPerEndpoint="65536" />
</tcpTransport>
</binding>
</customBinding>
</bindings>
我已经更新了服务引用并尝试更改了一些缓冲区和大小,但是它没有用(不是因为只返回bool而我预计会出现问题)。
修正使用:
OperationContext.Current.OperationCompleted += Test;
private void Test(object sender, EventArgs e)
{
OperationContext.Current.Channel.Close();
}
不要使用Abort而是使用Close方法!
答案 0 :(得分:1)
我发现了问题:我使用
中止了服务器端的频道OperationContext.Current.OperationCompleted
其中:
OperationContext.Current.Channel.Abort();
这似乎在99%的情况下都能正常工作,但不是全部,使用:
OperationContext.Current.Channel.Close();
完全解决了我的问题。