【问题标题】:C# WCF NetTcpBinding timeout not workingC# WCF NetTcpBinding 超时不起作用
【发布时间】:2016-03-10 12:49:19
【问题描述】:

我遇到了 NetTcpBinding 在初次调用时的打开、发送、接收和关闭超时问题([OperationContract(IsOneWay = true, IsInitiating = true)]) 目前我正在尝试连接到给定的 IPAddress,如果失败,我将连接到不同的地址。 这是因为我无法通过我的外部 IP 连接到我自己的服务器(由于我的路由器上的 NAT 环回?),而不是有 2 个版本(一个供公众使用,一个供我自己使用)我使用 Try-Catch 系统尝试连接到我的外部 IP,如果有效 -> 继续,如果失败 -> 尝试本地 IP -> 继续。

但问题是超时值。由于我在等待一个异常被抛出和捕获,我需要尽可能地降低这个值(最好是 5 秒)而不是默认的 ~20 秒。

我尝试在客户端和服务器端设置这些值

tcpBinding.OpenTimeout = new TimeSpan(0, 0, 5);
tcpBinding.CloseTimeout = new TimeSpan(0, 0, 5);
tcpBinding.SendTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);

服务器端设置:

svh = new ServiceHost(typeof(ServiceAssembly.ServiceImplementation));
NetTcpBinding tcpBinding = new NetTcpBinding();

tcpBinding.MaxConnections = 100;
tcpBinding.MaxBufferPoolSize = (int)4096000;
tcpBinding.MaxBufferSize = 4096000;
tcpBinding.MaxReceivedMessageSize = (int)4096000;
tcpBinding.OpenTimeout = new TimeSpan(0, 0, 5);
tcpBinding.CloseTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
tcpBinding.SendTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReliableSession.Ordered = true;
tcpBinding.TransactionFlow = false;
tcpBinding.Security.Transport.ProtectionLevel =
System.Net.Security.ProtectionLevel.EncryptAndSign;
tcpBinding.Security.Transport.ClientCredentialType =
TcpClientCredentialType.Windows;
tcpBinding.Security.Mode = SecurityMode.None;

svh.AddServiceEndpoint(typeof(ServiceAssembly.IChat),tcpBinding,"net.tcp://IPAddress:3100/MyService");

SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
svh.Open();

客户端设置和调用

InstanceContext context = new InstanceContext(callback);
NetTcpBinding tcpBinding = new NetTcpBinding();

tcpBinding.MaxConnections = 100;
tcpBinding.MaxBufferPoolSize = (int)4096000;
tcpBinding.MaxBufferSize = 4096000;
tcpBinding.MaxReceivedMessageSize = (int)4096000;
tcpBinding.TransactionFlow = false;
tcpBinding.OpenTimeout = new TimeSpan(0, 0, 5);
tcpBinding.CloseTimeout = new TimeSpan(0, 0, 5);
tcpBinding.SendTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReliableSession.Ordered = true;

tcpBinding.Security.Transport.ProtectionLevel =
    System.Net.Security.ProtectionLevel.EncryptAndSign;
tcpBinding.Security.Transport.ClientCredentialType =
    TcpClientCredentialType.Windows;
tcpBinding.Security.Mode = SecurityMode.None;

 try
 {
      scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
            "net.tcp://" + m_ipAddress + ":" + m_port + "/MyService");
      s = scf.CreateChannel(); 
      s.Connect(client); <- First connection
 }
 catch
 {
     try
     {
         scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
               "net.tcp://" + m_localAddress + ":" + m_port + "/MyService");
         s = scf.CreateChannel();
         s.Connect(client);
         m_ipAddress = m_localAddress;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }

但是抛出异常的超时时间仍然在 20 秒左右。更改这些值不会更改引发异常的时间。我在下面添加了一张图片来显示调用堆栈。 需要任何有关如何解决此问题甚至使其变得更好的建议。

【问题讨论】:

  • 你确定它使用了 tcpbinding 设置,例如,它周围还有什么代码,显示它正在设置,使用这个绑定,然后打开。
  • 抛出了什么异常?
  • @BugFinder 查看我的编辑。
  • @Helic 它在预期的图像“System.ServiceModel.EndpointNotFoundException”中。
  • 我认为“OpenTimeout”就是这样,如果没有找到,dns 和其他各种意味着没有端点可以使用的东西不会被这些超时覆盖,只有一个连接应该工作的必须在这个时间限制内连接。

标签: c# wcf tcp nettcpbinding


【解决方案1】:

我发现对我有帮助的解决方案——不是删除这个超时,而是连接到我自己的服务器(如果不是本地服务器和/或通过外部 IP 连接)——实际上是寻找进程本身。如果找到 -> 本地连接,否则 -> 通过给定 IP 连接。 这并不能回答我的实际问题 - 但是 - 它确实解决了我最初的问题。 似乎 TimeOut “延迟”是由 DNS 引起的,我无法阻止它。

如果有人感兴趣,这是我的解决方案代码。

if (Process.GetProcessesByName("ProcessName").Length > 0)
     isLocal = true;

 if (isLocal)
     scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
           "net.tcp://" + m_localAddress + ":" + m_port + "/MyService");
 else
     scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
           "net.tcp://" + m_ipAddress + ":" + m_port + "/MyService");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2011-02-14
    相关资源
    最近更新 更多