gpt4 book ai didi

.net - WCF 客户端在服务关闭时忽略超时值

转载 作者:行者123 更新时间:2023-12-04 02:23:36 24 4
gpt4 key购买 nike

我有一个使用 WCF 的 VB .NET 应用程序。我已经为代码中的所有内容设置了客户端超时:

    Dim oMastSrv As MastSvc.IclsIOXferClient = Nothing

Dim binding As New ServiceModel.NetTcpBinding("NetTcpBinding_IclsIOXfer")
Dim intTimeout As Integer = 2500
binding.SendTimeout = New TimeSpan(0, 0, 0, 0, intTimeout)
binding.ReceiveTimeout = New TimeSpan(0, 0, 0, 0, intTimeout)
binding.OpenTimeout = New TimeSpan(0, 0, 0, 0, intTimeout)
binding.CloseTimeout = New TimeSpan(0, 0, 0, 0, intTimeout)
Dim address As New ServiceModel.EndpointAddress("net.tcp://" & GetSrvIP(intSrvID) & ":30000/MyMastSvc")

oMastSrv = New MastSvc.IclsIOXferClient(binding, address)
Try
oMastSrv.ServiceConnect( ... )
oMastSrv.InnerChannel.OperationTimeout = New TimeSpan(0, 0, 0, 0, intTimeout)
Catch ex As Exception
...
End Try

但是,当我连接到的服务崩溃时,Endpoint Not Found 异常需要超过 20 秒才能被抛出,而不是我指定的 2.5。这对我的负载平衡来说真的很糟糕,我需要知道服务在 2.5 秒内消失。有没有办法让这个异常在所需的时间范围内抛出?

顺便说一句,异常内容如下:

Could not connect to net.tcp://192.168.227.130:30000/MXIOXfer. The connection attempt lasted for a time span of 00:00:02.4209684. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.227.130:30000.



但它确实需要超过 20 秒。我已经打开了 WCF 跟踪,并且可以在异常之前看到 TCP 操作失败警告,并且它具有实时时间:

Could not connect to net.tcp://192.168.227.130:30000/MXIOXfer. The connection attempt lasted for a time span of 00:00:21.0314092. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.227.130:30000.



如果有任何区别,服务的所有通信都在单独的线程上完成。

编辑:

This thread似乎表明套接字超时是由操作系统设置的。是否有此类事情的注册表设置?

最佳答案

结合我和 eol 引用的 SO 和 MSDN Social 线程中的详细信息,我找到了以下注册表设置:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces{xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx}\TcpInitialRTT

Value Type: REG_DWORD—number

Valid Range: 0–0xFFFF

Default: 3 seconds

Description: This parameter controls the initial time-out used for a TCP connection request and initial data retransmission on a per-interface basis. Use caution when tuning with this parameter because exponential backoff is used. Setting this value to larger than 3 results in much longer time-outs to nonexistent addresses.



.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces{xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx}\TcpMaxConnectRetransmissions

Value Type: REG_DWORD—number

Valid Range: 0–255 (decimal)

Default: 2

Description: This parameter determines the number of times that TCP retransmits a connect request (SYN) before aborting the attempt. The retransmission time-out is doubled with each successive retransmission in a given connect attempt. The initial time-out is controlled by the TcpInitialRtt registry value.



.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces{xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx}\TcpMaxDataRetransmissions

Value Type: REG_DWORD—number

Valid Range: 0–0xFFFFFFFF

Default: 5

Description: This parameter controls the number of times that TCP retransmits an individual data segment (not connection request segments) before aborting the connection. The retransmission time-out is doubled with each successive retransmission on a connection. It is reset when responses resume. The Retransmission Timeout (RTO) value is dynamically adjusted, using the historical measured round-trip time (Smoothed Round Trip Time, or SRTT) on each connection. The starting RTO on a new connection is controlled by the TcpInitialRtt registry value.



由于每次重试失败连接的超时值加倍,默认值使第一次尝试在 3 秒内失败,第二次在 6 秒内失败,第三次也是最后一次尝试在 12 秒内失败,或总共 21 秒。顺便说一句,TcpMaxDataRetransmissions 键与此无关,我将其包括在内以保持完整性和稍后出现的人。

默认情况下,这些值都不存在,您必须添加它们才能更改它们。确定要在哪个接口(interface)上执行此操作很容易,每个接口(interface)都有一个包含其当前 IP 地址的 key 。 (甚至 localhost 也有一个。)在我自己的情况下,只需在 VM 接口(interface)上将 TcpMaxConnectRetransmissions 设置为零 (0),我的套接字超时默认为 3 秒,这足以接近 2.5 以工作。当 WCF 服务现在崩溃时,我的负载平衡工作。

关于.net - WCF 客户端在服务关闭时忽略超时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11902985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com