繁体   English   中英

WCF超时问题

[英]WCF Timeout Problem

我有以下代码

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属性),并确保该服务能够连接到客户端。

我如何以与避免这种错误的方式不同的方式投射频道,这是有道理的,因为基础请求可能要花一分钟多的时间才能计算出来。

将客户端操作超时设置为

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

这将消除您的错误。

也尝试设置ReceiveTimeout属性。

暂无
暂无

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

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