【发布时间】: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