目标计算机未运行时的WCF超时

时间:2016-03-07 16:00:45

标签: c# .net wcf

我的.NET 4.5应用程序使用WCF net.tcp绑定与服务器进行通信。沟通非常简单。客户端只调用一个方法,服务器返回true / false。服务器必须在5秒内响应。如果不是客户端尝试另一台服务器。时机对我来说至关重要。    当具有服务器的PC运行时,WCF超时(发送,接收,打开,关闭,操作和ChannelInitializationTimeout)正常工作。但是,当PC未运行(或配置中填写了错误的IP地址)时,它将花费近30秒直到抛出异常。我必须配置其他超时才能使其正常工作吗?

这是我的客户端代码(没有任何内容放在app.config文件中):

        NetTcpBinding binding = new NetTcpBinding(SecurityMode.None)
        {
            SendTimeout = TimeSpan.FromMilliseconds(Configuration.Instance.LocalConfiguration.Failover.SendTimeout),
            ReceiveTimeout = TimeSpan.FromMilliseconds(Configuration.Instance.LocalConfiguration.Failover.RecieveTimeout),
            OpenTimeout = TimeSpan.FromMilliseconds(Configuration.Instance.LocalConfiguration.Failover.OpenTimeout),
            CloseTimeout = TimeSpan.FromMilliseconds(Configuration.Instance.LocalConfiguration.Failover.CloseTimeout),
            TransactionFlow = false,
            TransactionProtocol = TransactionProtocol.Default,
            TransferMode = TransferMode.Buffered,
            Security = new NetTcpSecurity() { Mode = SecurityMode.None },
            ReliableSession = new OptionalReliableSession() { Enabled = false },
            ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxArrayLength = 16384, MaxBytesPerRead = 4096, MaxDepth = 32, MaxNameTableCharCount = 16384, MaxStringContentLength = 8192 },
            Name = "NoSecurity",
            MaxReceivedMessageSize = 65535,
            MaxConnections = 10,
            MaxBufferSize = 65535,
            MaxBufferPoolSize = 524288,
            ListenBacklog = 10,
            HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
        };

        BindingElementCollection be = binding.CreateBindingElements();
        TcpTransportBindingElement tcpBe = be.Find<TcpTransportBindingElement>();
        tcpBe.ChannelInitializationTimeout = TimeSpan.FromMilliseconds(Configuration.Instance.LocalConfiguration.Failover.InitializationTimeout);
        tcpBe.TransferMode = TransferMode.Buffered;
        CustomBinding customBinding = new CustomBinding(be);

        FailoverClient.ListenerClient serviceClient = new FailoverClient.ListenerClient(customBinding, new EndpointAddress(address));
        serviceClient.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(Configuration.Instance.LocalConfiguration.Failover.OperationTimeout);

ps:30秒后抛出的异常是'System.ServiceModel.EndpointNotFoundException ...尝试持续00:00:04.123。 TCP错误10060 ...'与嵌套异常'System.Net.Sockets.SocketException ...远程方在一段时间后没有正确响应......'

修改 我找到了一个解决方法,但它没有回答我的问题。我可以使用异步调用并等待完成。

        Task<bool> task = serviceClient.HeartbeatAsync();
        try
        {
            if (task.Wait(5000))
            {
                Console.WriteLine("Task result: " + task.Result);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

0 个答案:

没有答案
相关问题