【问题标题】:How can I get a timeout exception in WCF using TcpClient Class如何使用 TcpClient 类在 WCF 中获取超时异常
【发布时间】:2014-06-30 23:30:22
【问题描述】:

我遇到了一个问题。我的合同实施(除了其他东西)是这样的:

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();
                    }

using 关键字下的部分永远不会被调用,因为内置的 WCF 客户端在 TCPClient 部分上“挂起”,因此会引发 SocketExeption 错误。我已将 Web 配置中的超时设置为 5 秒。我想要实现的不是抛出套接字异常而是抛出超时异常。看起来抛出了 SocketException,但我不能让我的 wcf 服务抛出超时异常。有可能这样做吗?我希望我的任务是可以理解的,我想做什么。如果不是,我会尽量解释清楚。

【问题讨论】:

    标签: c# wcf sockets tcp tcpclient


    【解决方案1】:

    TcpClient 不知道也不关心你在说什么。它没有 WCF、Web 服务或您想要的 TimeoutException 的概念。

    它所做的一切就是维持一个 TCP 连接。

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


    并摆脱这种迷信的处置东西:

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

    一次就够了。

    【讨论】:

    • 感谢您的回答我会尝试您的解决方案,但是,怎么做?错误代码是指 SocketExeption 的属性?如果是的话,有没有表参考超时时会生成什么代码?
    • 好吧,我想我明白了,我已经用谷歌搜索了它,我认为在 Socket 异常下我会做这样的事情: if (ex.SocketErrorCode == SocketError.TimedOut) throw new TimeoutException(" TireaSinco 服务超时");你是这个意思吗?
    • 好的! :) 最后一个问题是。在您看来,在这种情况下(即使用此错误代码检查),我是否需要在 WCF 上配置超时?
    • 好吧,另一件事。我正在使用 WCF Test clinet 进行测试,它有自己的配置文件(看起来好像有)。在那里我已经设置了我期待超时的时间,它也可以工作:) 谢谢你的帮助。您的解决方案也需要:)
    • 是的,这就是我的意思。您可以配置所需的任何 WCF 超时。我认为那里没有任何问题。
    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2018-11-22
    • 1970-01-01
    • 2010-11-02
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多