简体   繁体   English

WCF超时问题

[英]WCF Timeout Problem

I have the following code 我有以下代码

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;
        }
    }

and I occasionally get the following error when the underlying process takes more than a minute. 当基础流程花费一分钟以上的时间时,有时会出现以下错误。

Message: 信息:

This request operation sent to net.tcp://127.0.0.1:8000/VSMDBCommunication did not receive a reply within the configured timeout (00:01:00). 发送到net.tcp://127.0.0.1:8000 / VSMDBCommunication的此请求操作在配置的超时(00:01:00)内未收到答复。 The time allotted to this operation may have been a portion of a longer timeout. 分配给该操作的时间可能是较长超时的一部分。 This may be because the service is still processing the operation or because the service was unable to send a reply message. 这可能是因为该服务仍在处理该操作,或者是因为该服务无法发送回复消息。 Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client. 请考虑增加操作超时(通过将通道/代理转换为IContextChannel并设置OperationTimeout属性),并确保该服务能够连接到客户端。

How can I cast the channel in a different way than I am doing to avoid this error, which makes sense because the underlying request can take slightly over a minute to compute. 我如何以与避免这种错误的方式不同的方式投射频道,这是有道理的,因为基础请求可能要花一分钟多的时间才能计算出来。

Set the client operation time out as 将客户端操作超时设置为

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

This will remove your error. 这将消除您的错误。

也尝试设置ReceiveTimeout属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM