运维开发网

.net – 使用NetTcpBinding中止套接字连接

运维开发网 https://www.qedev.com 2020-07-09 19:12 出处:网络 作者:运维开发网整理
我知道已经有一些关于这个问题的问题,但没有回复解决了我的问题. 我在运行的服务器上有一个NetTcpBinding,并且不时(不总是,像20到60个调用中的1个,完全随机)我在客户端收到此异常: The socket connection was aborted. This could be caused by an error processing your message or a recei
我知道已经有一些关于这个问题的问题,但没有回复解决了我的问题.

我在运行的服务器上有一个NetTcpBinding,并且不时(不总是,像20到60个调用中的1个,完全随机)我在客户端收到此异常:

The socket connection was aborted. This could be caused by an error

processing your message or a receive timeout being exceeded by the

remote host, or an underlying network resource issue. Local socket

timeout was ’00:01:00′.

我注意到,当客户端的连接速度很慢时,这种异常会更频繁地发生.

此外,所述超时时间为1分钟,但异常已在1秒左右后发生.

我所做的:

var client = GetClient();
client.Open();
bool success = client.Call(); // Exception occurs here (the return value is a bool)

我有时也会在服务器端获得此异常:

A TCP error (995: The I/O operation has been aborted because of either

a thread exit or an application request) occurred while transmitting

data.

我启用了服务器跟踪,但是我得到了相同的例外,并没有为我提供任何额外的见解.

我的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方法!

我发现了问题:我在服务器端中止了通道

OperationContext.Current.OperationCompleted

它叫:

OperationContext.Current.Channel.Abort();

这似乎在99%的情况下都能正常工作,但不是全部,使用:

OperationContext.Current.Channel.Close();

完全解决了我的问题.

0

精彩评论

暂无评论...
验证码 换一张
取 消