我知道已经有很多关于这个问题的问题,但没有回复解决我的问题。
我在服务器上有一个 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>
我已经更新了服务参考并尝试更改一些缓冲区和大小,但它没有工作(我不希望那里出现问题,因为只返回一个布尔值)。
固定使用:
OperationContext.Current.OperationCompleted += Test;
private void Test(object sender, EventArgs e)
{
OperationContext.Current.Channel.Close();
}
不要使用 Abort 而是使用 Close 方法!