WCF超时问题

时间:2011-02-24 16:25:31

标签: c# wcf timeout

我有以下代码

public bool StartWCF()
    {
        try
        {
            // Select the first entry. I hope it's this maschines IP
            // IPAddress _ipAddress = ips.AddressList[0];
            var ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });

            // Create the url that is needed to specify where the service should be started
            this.m_UrlMetaServiceComm = "net.tcp://" + ipAddress + ":8000/VSMDBCommunication";
            this.m_UrlMetaServicePart = "net.tcp://" + ipAddress + ":8000/VSMDBPartType";

            string endPointAddrComm = this.m_UrlMetaServiceComm;
            var tcpBindingComm = new NetTcpBinding
                {
                    TransactionFlow = false,
                    MaxReceivedMessageSize = 20000000,
                    MaxBufferSize = 20000000,
                    MaxBufferPoolSize = 20000000,
                    ReaderQuotas = { MaxNameTableCharCount = 20000000 },
                    OpenTimeout = new TimeSpan(0, 5, 0),
                    SendTimeout = new TimeSpan(0, 5, 0),
                    CloseTimeout = new TimeSpan(0, 5, 0)
                };
            tcpBindingComm.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;
            tcpBindingComm.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            tcpBindingComm.Security.Mode = SecurityMode.None;

            var endpointAddressComm = new EndpointAddress(endPointAddrComm);
            this.m_ChannelCommunication = ChannelFactory<IVSMDBCommunication>.CreateChannel(
                tcpBindingComm, endpointAddressComm);
            ((IContextChannel)m_ChannelCommunication).OperationTimeout = new TimeSpan(0, 5, 0);
            string endPointAddrPart = this.m_UrlMetaServicePart;
            var tcpBindingPart = new NetTcpBinding
                {
                    TransactionFlow = false,
                    MaxReceivedMessageSize = 20000000,
                    MaxBufferSize = 20000000,
                    MaxBufferPoolSize = 20000000,
                    ReaderQuotas = { MaxNameTableCharCount = 20000000 },
                    OpenTimeout = new TimeSpan(0, 5, 0),
                    SendTimeout = new TimeSpan(0, 5, 0),
                    CloseTimeout = new TimeSpan(0, 5, 0)
                };
            tcpBindingPart.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;
            tcpBindingPart.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            tcpBindingPart.Security.Mode = SecurityMode.None;

            var endpointAddressPart = new EndpointAddress(endPointAddrPart);
            this.m_ChannelPartTypes = ChannelFactory<IVSMDBPartType>.CreateChannel(
                tcpBindingPart, endpointAddressPart);
            ((IContextChannel)m_ChannelPartTypes).OperationTimeout = new TimeSpan(0, 5, 0);
            return true;
        }
        catch (CommunicationObjectFaultedException faultEx)
        {
            // System.Diagnostics.Trace.TraceError(faultEx.ToString());
            Console.WriteLine("An unknown exception was received. " + faultEx.Message + faultEx.StackTrace);
            Console.Read();
            return false;
        }
        catch (EndpointNotFoundException endEx)
        {
            // System.Diagnostics.Trace.TraceError(endEx.ToString());
            Console.WriteLine("An unknown exception was received. " + endEx.Message + endEx.StackTrace);
            Console.Read();
            return false;
        }
    }

当底层进程花费超过一分钟时,我偶尔会遇到以下错误。

消息:

  

此请求操作发送到   的net.tcp://127.0.0.1:8000 / VSMDBCommunication   没有得到回复   配置超时(00:01:00)。该   分配给此操作的时间可能   是一段时间的一部分   超时。这可能是因为   服务仍在处理中   操作或因为服务   无法发送回复消息。   请考虑增加   操作超时(通过强制转换   通道/代理到IContextChannel和   设置OperationTimeout属性)   并确保服务能够   连接到客户端。

如何以不同于我正在做的方式转换频道以避免此错误,这是有道理的,因为基础请求可能需要稍微超过一分钟来计算。

2 个答案:

答案 0 :(得分:2)

将客户端操作时间设置为

client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(10);

这将删除您的错误。

答案 1 :(得分:0)

尝试设置ReceiveTimeout属性。