我知道已经有很多关于这个问题的问题,但没有回复解决了我的问题。
我在正在运行的服务器上有一个 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 操作已中止 线程退出或应用程序请求)在传输时发生 数据。
启用了服务器跟踪,但我收到相同的异常,并且它没有为我提供任何其他见解。
我的获取客户端:
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>
我已经更新了服务参考并尝试更改一些缓冲区和大小,但它不起作用(并不是说我期望那里有问题,因为只返回一个布尔值)。
固定使用:
OperationContext.Current.OperationCompleted += Test;
private void Test(object sender, EventArgs e)
{
OperationContext.Current.Channel.Close();
}
不要使用中止,而是使用关闭方法!
我发现了问题:我中止了服务器端的通道
OperationContext.Current.OperationCompleted
它叫:
OperationContext.Current.Channel.Abort();
这似乎在 99% 的情况下工作正常,但不是全部,使用:
OperationContext.Current.Channel.Close();
完全解决了我的问题。