Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF:超时错误_C#_.net_Wcf_.net 3.5 - Fatal编程技术网

C# WCF:超时错误

C# WCF:超时错误,c#,.net,wcf,.net-3.5,C#,.net,Wcf,.net 3.5,我有一段代码可以调用托管在服务器上的WCF服务 代码不停地循环调用这个方法。(它要求一个“状态”,所以它根本不做任何工作) 没关系,只是过了一小段时间后我出现了一个错误: 此请求操作已发送到网络。tcp://serverName:9001/service1 在配置的超时内未收到答复(00:00:09.9843754) 突然之间,我根本无法访问服务器。我将超时时间增加到1分钟,但仍然存在相同的问题。请注意,承载服务的程序不做任何其他事情,只提供它的“状态”。因此,WCF服务应用程序正忙,这不是问题

我有一段代码可以调用托管在服务器上的WCF服务

代码不停地循环调用这个方法。(它要求一个“状态”,所以它根本不做任何工作)

没关系,只是过了一小段时间后我出现了一个错误:
此请求操作已发送到网络。tcp://serverName:9001/service1 在配置的超时内未收到答复(00:00:09.9843754)

突然之间,我根本无法访问服务器。我将超时时间增加到1分钟,但仍然存在相同的问题。请注意,承载服务的程序不做任何其他事情,只提供它的“状态”。因此,WCF服务应用程序正忙,这不是问题

我认为调用服务的代码有问题,因为当我重新启动应用程序时,它可以很好地连接到服务。。。直到又过了一小段时间,我再次出现超时错误。出于这个原因,我也不认为这是网络错误,因为当我重新启动应用程序时,它在一段时间内是正常的

这是我用来调用服务的代码。我是否需要在每次呼叫后处理ChannelFactory来清理它,或者我正在做什么

        NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
                binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

                EndpointAddress endPoint = new EndpointAddress(new Uri(clientPath));

                ChannelFactory<IClient> channel = new ChannelFactory<IClient>(binding, endPoint);
                channel.Faulted += new EventHandler(channel_Faulted);
                IClient client = channel.CreateChannel();

                ((IContextChannel)client).OperationTimeout = new TimeSpan(0, 0, 10);
                ClientStatus clientStatus = client.GetStatus();
NetTcpBinding=newnettcpbinding(SecurityMode.Message);
binding.Security.Message.ClientCredentialType=MessageCredentialType.Windows;
EndpointAddressEndpoint=新的EndpointAddress(新Uri(clientPath));
ChannelFactory通道=新的ChannelFactory(绑定,端点);
channel.Faulted+=新的EventHandler(channel_Faulted);
IClient client=channel.CreateChannel();
((IContextChannel)客户端)。操作超时=新的时间跨度(0,0,10);
ClientStatus ClientStatus=client.GetStatus();

在调用完GetStatus后,您必须关闭客户端连接。最好的方法是使用using块。但是您也可以在调用client.GetStatus()之后执行类似的操作


这将不起作用,因为如果状态出错,将调用neighter Close()或About()。在调用Close()之前,应该将client.GetState()移动到try块中,或者删除密码。
ClientStatus clientStatus = client.GetStatus();

try
{
    if (client.State != System.ServiceModel.CommunicationState.Faulted)
    {
         client.Close();
    }
}
catch (Exception ex)
{
    client.Abort();
}