2014-06-30 65 views
1

我遇到了一些特定的问题。我的合同执行(除了其他的东西),有这样的事情:在如何在WCF中使用TcpClient类获取超时异常

_socketRequest.SendRequest(请求,emmiterHeader)

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

即可;

在其他的东西像下面的东西:

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客户端上的TcpClient部分“悬”,whcich的结论提出了一个SocketExeption错误。我已经在web配置中设置了超时时间,可以说5秒。我喜欢的是抛出套接字异常,但超时异常。它看起来像是抛出了SocketException,但我无法让我的wcf服务抛出超时异常。有可能这样做吗?我希望我的追求能够理解我想做的事情。如果没有,我会尽我所能解释清楚。

回答

0

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

所有它保持TCP连接。

抓住SocketException并分析存储在其中的错误代码。有一个超时代码。自己投掷TimeoutException


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

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

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

一次就够了。

+0

谢谢你的答案我会尝试你的解决方案,但是,怎么做呢?通过错误代码你是指SocketExeption的属性?如果有的话,是否有任何表参考当超时时产生什么代码? –

+0

好吧,我想我知道了,我已经gooogled它,我认为,在Socket异常我会做这样的事情:if(ex.SocketErrorCode == SocketError.TimedOut) throw new TimeoutException(“TireaSinco service timeout”);这是你的意思吗? –

+0

好吧,它的作品! :)最后一个最后的问题是。在这种情况下(即使用此错误代码检查),我需要在WCF超时配置? –