简体   繁体   English

WCF:超时错误

[英]WCF: Timeout error

I have a piece of code that calls a WCF service that is hosted on a server. 我有一段代码可以调用服务器上托管的WCF服务。

The code keeps looping around and around calling this method over and over again. 代码不断地反复遍历该方法。 (It's asking for a 'status', so it's not doing any work at all). (它要求一个“状态”,所以它根本不做任何工作)。

That's fine except that after a short period of time I get an error: 没关系,但短时间后出现错误:
This request operation sent to net.tcp://serverName:9001/service1 did not receive a reply within the configured timeout (00:00:09.9843754) 发送到net.tcp:// serverName:9001 / service1的此请求操作在配置的超时(00:00:09.9843754)内未收到答复

And suddenly i cannot get to the server at all EVER. 突然之间,我根本无法访问服务器。 I increased the timeout to 1min but still the same problem. 我将超时时间增加到1分钟,但仍然是同样的问题。 Note that the program that hosts the service is doing nothing else, just offering it's 'status'. 请注意,托管服务的程序没有做任何其他事情,只是提供了“状态”。 So it's not an issue with the WCF service app being busy. 因此,WCF服务应用程序忙起来不是问题。

I think it's a problem with the code calling the service because when i re-start the app it can connect to the service just fine ... until after another short time i get the timeout error again. 我认为这是调用服务的代码的问题,因为当我重新启动应用程序时,它可以正常连接到服务...直到再过一小段时间后,我再次收到超时错误。 For this reason i don't thnk it's a network error either, as when I restart the app it's ok for a period of time. 出于这个原因,我也不认为这是网络错误,因为当我重新启动应用程序一段时间后就可以了。

Here is the code i use to call the service. 这是我用来致电服务的代码。 Do i need to dispose of the ChannelFactory after each call to clean it up or what am i doing worng? 每次调用后我都需要处置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();

You do have to close client connections after you finish calling GetStatus. 完成调用GetStatus后,您必须关闭客户端连接。 The best way to do this is to use a using block. 最好的方法是使用using块。 But you can also do something like this after your call client.GetStatus() 但是您也可以在呼叫客户端之后执行类似的操作。GetStatus()

ClientStatus clientStatus = client.GetStatus();

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

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

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