简体   繁体   English

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

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

I have a certain issue which I'm facing. 我面临一个特定的问题。 My contract implementation (besides other stuff) has something like this: 我的合同实施(除其他事项外)具有以下内容:

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

Then in 然后在

_socketRequest.SendRequest(request, emmiterHeader); _socketRequest.SendRequest(request,emmiterHeader);

Among other stuff is something like below: 除其他外,如下所示:

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

The part under the using key word is never called because built in WCF Client is "hanging" on the TCPClient part, whcich in conclusion raises a SocketExeption error. 使用关键字下的部分永远不会被调用,因为内置的WCF Client在TCPClient部分上是“挂起”的,因此最终会引发SocketExeption错误。 I have set the timeouts in the web config to lets say 5 seconds. 我已将Web配置中的超时设置为可以说5秒。 What I like to achieve is to throw not socket exception but the timeout exception. 我想实现的是抛出套接字异常而不是超时异常。 It looks like the SocketException is thrown but I can't make my wcf service throw a timeout exception. 看起来好像抛出了SocketException,但是我无法使wcf服务抛出超时异常。 Is it possible to do that? 有可能这样做吗? I hope my questin is understandable what I want to do. 我希望我的询问是可以理解的。 If not I will try to explain as clearly as I can. 如果没有,我会尽力解释清楚。

TcpClient does not know or care what you talk to. TcpClient不知道或不在乎您在说什么。 It has no notion of WCF, web services or the TimeoutException that you want. 它没有WCF,Web服务或所需的TimeoutException概念。

All it does it maintain a TCP connection. 它所做的所有事情都会维持TCP连接。

Catch the SocketException and analyze the error code stored in it. 捕获SocketException并分析其中存储的错误代码。 There is a code for timeouts. 有一个超时代码。 Throw TimeoutException yourself. 自己抛出TimeoutException


And get rid of this superstitious dispose stuff: 并摆脱这种迷信的处理方法:

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

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

Once is enough. 一次就够了。

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

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