0

我已经用这些超时配置了我的 wcf:

  public static NetTcpBinding MakeTcpBinding(bool isClient)
  {
     return new NetTcpBinding(SecurityMode.None, false)
     {
        HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
        TransactionFlow = false,
        PortSharingEnabled = true,
        Security = {Transport = {ClientCredentialType = TcpClientCredentialType.None}},
        ReceiveTimeout = isClient ? TimeSpan.FromSeconds(5) : TimeSpan.FromDays(1),
        SendTimeout = isClient ? TimeSpan.FromSeconds(1) : TimeSpan.FromSeconds(5),
        OpenTimeout = TimeSpan.FromSeconds(1),
        CloseTimeout = TimeSpan.FromSeconds(5),
        MaxReceivedMessageSize = int.MaxValue,
        ListenBacklog = int.MaxValue,
        MaxBufferSize = int.MaxValue,
        MaxBufferPoolSize = 524288,
        ReaderQuotas =
        {
           MaxArrayLength = int.MaxValue,
           MaxStringContentLength = int.MaxValue,
           MaxDepth = int.MaxValue,
           MaxBytesPerRead = int.MaxValue,
           MaxNameTableCharCount = int.MaxValue
        }
     };
  }

当客户端连接到不存在的服务器时,应该将 imeout 设置为 1 秒。

但是,当实际调用 open 时,我看到 21 秒超时。

调试 2013-07-31 18:12:44,712 Communication.WcfCommunicator - NotifyAllReceivers:类型:AskWhoIsAwake,发送日期:2013 年 7 月 31 日上午 18:12:44,DetectSessionId:37106dee-b563-458b-9eb7-a90e81f82563 - 1

调试 2013-07-31 18:13:05,746 Communication.WcfCommunicator - 无法连接到 net.tcp://notup/xxx.svc/motup_www-xxx-com。连接尝试持续了 00:00:00.9879988 的时间跨度。TCP错误码10060:连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应42.42.42.42:808。- 6

是什么导致额外的 20 秒超时?

4

1 回答 1

1

使用异步打开和设置超时解决。

     var ar = client.InnerChannel.BeginOpen(null, null);
     if (!ar.AsyncWaitHandle.WaitOne(client.Endpoint.Binding.OpenTimeout, true))
        throw new CommunicationException("Service is not available");
     client.InnerChannel.EndOpen(ar);
于 2013-08-01T03:59:34.027 回答