繁体   English   中英

如何使用TcpClient类在WCF中获得超时异常

[英]How can I get a timeout exception in WCF using TcpClient Class

我面临一个特定的问题。 我的合同实施(除其他事项外)具有以下内容:

try
            {

                response = _socketRequest.SendRequest(request, emmiterHeader);
                trCode = response.TraceCodeInt;
                if (trCode == 0)
                    statusRequest = (int) TSRequestAttemptStatus.active;
            }
            catch (TimeoutException tex)
            {
                throw;
            }
            catch (Exception)
            {
                statusRequest = (int) TSRequestAttemptStatus.notActive;
                throw;
            }
            finally
            {
                tsra = CreateTireaSincoRequestAttemptRow(DateTime.Now, statusRequest, emmiterHeader.HeaderId,
                                                         string.Empty,
                                                         string.Empty,
                                                         string.Empty,
                                                         previousContractNumber,
                                                         taxIdentificationCode,
                                                         policyHolderName,
                                                         policyHolderFirstName,
                                                         policyHolderLastName,
                                                         registrationNumberType.Substring(0, 1),
                                                         registrationNumber,
                                                         ((byte) ControlCodes.F),
                                                         DateTime.Now.AddDays(emmiterHeader.ValidityPeriod));
            }

然后在

_socketRequest.SendRequest(request,emmiterHeader);

除其他外,如下所示:

using (var client = new TcpClient(header.SocketServerAddress,
                                      header.SocketServerPort == null ? 1 : (int)header.SocketServerPort))
                    {

                   Socket socket = client.Client;

                        // send data with timeout 10s
                        //socket.Send(arr);

                        Send(socket, arr, 0, arr.Length, 1000);

                        if (header.DebugMode)
                            _logger.LogInfo(InterfaceName, string.Format("Data Socket Send: {0} ", tempArr));
                        // receive data with timeout 10s
                        //Receive(client, arr);

                        len = Receive(socket, arrResponse, 0, arrResponse.Length, 5000, header.DebugMode, _logger);

                        if (socket.Connected)
                            socket.Close();

                        if (client.Connected)
                            client.Close();
                    }

使用关键字下的部分永远不会被调用,因为内置的WCF Client在TCPClient部分上是“挂起”的,因此最终会引发SocketExeption错误。 我已将Web配置中的超时设置为可以说5秒。 我想实现的是抛出套接字异常而不是超时异常。 看起来好像抛出了SocketException,但是我无法使wcf服务抛出超时异常。 有可能这样做吗? 我希望我的询问是可以理解的。 如果没有,我会尽力解释清楚。

TcpClient不知道或不在乎您在说什么。 它没有WCF,Web服务或所需的TimeoutException概念。

它所做的所有事情都会维持TCP连接。

捕获SocketException并分析其中存储的错误代码。 有一个超时代码。 自己抛出TimeoutException


并摆脱这种迷信的处理方法:

                    if (socket.Connected)
                        socket.Close();

                    if (client.Connected)
                        client.Close();

一次就够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM